7.Question 7Consider this code:12age1 = input("How old are you? ")age2 = input("How old is your best friend? ")The user enters the ages in years as whole numbers (e.g., 2). Select the code fragment(s) that print the sum of the ages. Be sure to use Python 3.1 pointprint(int(age1) + int(age2))print(int(age1) + int(age2))print(age1 + age2)print(age1 + age2)print(str(int(age1 + age2)))print(str(int(age1 + age2)))x = int(age1)x = int(age1)y = int(age2)y = int(age2)print(str(x + y))print(str(x + y))
Question
7.Question 7Consider this code:12age1 = input("How old are you? ")age2 = input("How old is your best friend? ")The user enters the ages in years as whole numbers (e.g., 2). Select the code fragment(s) that print the sum of the ages. Be sure to use Python 3.1 pointprint(int(age1) + int(age2))print(int(age1) + int(age2))print(age1 + age2)print(age1 + age2)print(str(int(age1 + age2)))print(str(int(age1 + age2)))x = int(age1)x = int(age1)y = int(age2)y = int(age2)print(str(x + y))print(str(x + y))
Solution
The correct code fragments that print the sum of the ages are:
- print(int(age1) + int(age2))
- x = int(age1) y = int(age2) print(x + y)
These two fragments correctly convert the input strings to integers before adding them. The other fragments either don't convert the strings to integers, resulting in string concatenation instead of addition, or they convert the sum back to a string before printing, which is unnecessary.
Similar Questions
Could not pasteHailey is curious about the ages of her friends, Alex and Bob. She knows the sum of their ages and wants to determine their individual ages. The hint is that Alex is 5 years older than Bob. Can you help her create a program using pointers that calculates and prints the ages of Alex and Bob? Input format : The input consists of an integer N, representing the sum of Alex and Bob's ages. Output format : The first line of output prints the age of Alex. The second line prints the age of Bob. Refer to the sample output for formatting specifications. Code constraints : 1 ≤ N ≤ 100 Sample test cases : Input 1 : 45 Output 1 : Alex is 25 years old. Bob is 20 years old. Input 2 : 91 Output 2 : Alex is 48 years old. Bob is 43 years old.
Choose the code to assign your age into age variable?age=45print(age)age="45 "print(age)age=str(99)print(age)None of the above
In a school a class teacher wants to estimate age of the students in years, months and days. Date of birth will be given as input by the user in the format years-months-days. write a python program to estimate age of the student in the asked format. (Example: If the input is "1997-10-12", then the output of the program should be "26 years, 1 months, 13 days " )Input Format:Years-months-daysOutput Format:total years, total months, total days
Complete the main method in the AgeChecker class by Declaring and instantiating a Scanner objectPrint the "What is your age?" message to the consoleDeclaring the int age variable, assigning it the output of nextInt()If the value of age is greater than 40 then print "You are old". Otherwise, print "You are young"
The ratio of ages of two persons is 5 : 9 and the age of one of them is greater than the other by 40 years. The sum of their ages in year is?Options160150180140
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.