In: Computer Science
Write, save, and run a Python program
|
4 marks |
for i in range(0,10): 2 what is wrong in the above python code? display your explanation using print statement. |
|
3 insert the
following comments into your program of part 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. |
|
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
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