Question

In: Computer Science

Write, save, and run a Python program Ask the user to enter a number of grams....

Write, save, and run a Python program

  1. Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.

     4 marks

for i in range(0,10):
print(i)

2    what is wrong in the above python code?

display your explanation using print statement.

  1. mark

3    insert the following comments into your program of part
      (1). make sure the comment spans 3 lines of your code.

This program converts,

the number of seconds you have entered

into whole hours, then the whole minutes and whatever..

     2 mark

mark = mark1+ mark2 +

            mark3 + mark4

4   what is wrong in the above python code?

display your explanation using print statement.

  1. mark

5 The output of the program should look similar to the output of a sample run shown below.

      2 marks

10 marks total

#Sample run

Enter the weight in grams: 217123935

Total 217123 tones is 217 tones, 123 kilograms, 935 grams.

(you provide your answer for task 3)

This program converts,

the number of seconds you have entered

into whole hours, then the whole minutes and whatever..

(you provide your answer for task 4)

Process finished with exit code 0

This code should display the above answer

Solutions

Expert Solution

Python code


#Answer to Q1
#This Program Converts 
#given amount in grams to 
#tones, kilos and grams
val = int(input('Enter the weight in grams: ')) 
print("Total %d is %d tones, %d kilograms, %d grams" % (val, int(val/1000000), int(val/1000)%1000000, val%1000))

print()
#Answer to Q2
print("The code above is not indented properly.")
print("The intended purpose looks to print i from 0 to 9 but instead it will throw error as i is undeclared outside for loop.")
print("Corrected Code below.")
print("for i in range(0,10):")
print("    print(i)")

print()
#Answer to Q3
print("This Program Converts")
print("given amount in grams to")
print("tones, kilos and grams")

print()
#Answer to Q4
print("The given code either should span in a single line or use an implicit continuation symbol as shown below")
print("mark = mark1 + mark2 + \\")
print("    mark3 + mark4")

Sample Output

Explanation to Q3 and Q4 (also present in code)

Q2 -> The code above is not indented properly.The intended purpose looks to print i from 0 to 9 but instead it will throw error as i is undeclared outside for loop.

Q4 -> The given code either should span in a single line or use an implicit continuation symbol as shown below

mark = mark1 + mark2 + \
mark3 + mark4

Related Solutions

In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the user to enter the student names and scores as shown. Output the name and score of the student with the 2nd lowest score. NOTE: You may assume all students have distinct scores between 0 and 100 inclusive and there are at least 2 students NOTE: You may only use what has been taught in class so far for this assignment. You are not allowed...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
(8 marks) Write a program to ask user to enter an integer that represents the number...
Write a program to ask user to enter an integer that represents the number of elements, then generate an ArrayList containing elements which are all random integers in range [75, 144] , and finally display index and value of each element. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use ArrayList. Your program must use only printf(…) statements to adjust the alignment of your output. Your code must display the index in descending...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the...
PYTHON: Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663. This is my code, but I cannot figure out where to go from here. #set new number new_number = "" #split number split_num = phone.split("-") for char in split_num[1:2]:...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
In Python, write a program that allows the user to enter a number between 1-5 and...
In Python, write a program that allows the user to enter a number between 1-5 and returns the vitamins benefits based on what the user entered. The program should: Continue to run until the user quits and Validate the user’s input (the only acceptable input is 0, 1, 2, 3, 4, or 5), If the user enters zero “0” the program should terminate You can use the following facts: 1- Vitamin A protects eyes from night blindness and age-related decline...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT