6. Write a single statement that prompts the user to enter their age in years and places the integer value in a variable named age.
7. Can a program place more than one statement on the same program line?
8. Write a short code segment in Python that prompts the user to enter the number of quarters, dimes, nickels and pennies they have, compute the total value and print that value.
Answer 6:
age = int(input("Enter your age in years: "))
Explanation(on the left-hand side is the code and on the right-hand side is the output) :
Answer 7: Yes we can put more than one statement in a single line in python, but we will have to use a semicolon to separate the statements from each other. The image below shows the example for the same.
Answer 8: (Below is the screenshot of the code with an explanation of the left-hand side and on the right-hand side is the output of the code in action)
The Code Used:
quarters = int(input("Enter the number of quarters: ")) # Asking the user input dimes = int(input("Enter the number of dimes: ")) nickels = int(input("Enter the number of nickels: ")) pennies = int(input("Enter the number of pennies: ")) # 1 quarter = 0.25 dollar # 1 dimes = 0.1 dollar # 1 nickel = 0.05 dollar # 1 penny = 0.01 dollar totalDollars = quarters*0.25 + dimes*0.1 + nickels*0.05 + pennies*0.01 print("The total amount $"+str(totalDollars))
I hope you like the solution. In case of any doubts regarding the solution feel free to ask it in the comment section. If you like the solution please give a thumbs up.
Get Answers For Free
Most questions answered within 1 hours.