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

Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
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.
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
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.
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
(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)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT