IN PYTHON : Write a program that asks the user for a number. Write the number to a file called total.txt. Then ask the user for a second number. Write the new number on line 2 with the total for both numbers next to it separated by a comma. Then ask the user for a third number and do the same. Keep asking for numbers until the person puts in a zero to terminate the program. Close the file. Be sure to use at least 2 functions [main() and total_numbers(number, total)]. Put all inputs and outputs into the main function, with all calculations into the total_numbers function
Remember to do this in Python.
Python code screen shot:
Sample Input:
Sample Output:
Code to copy:
def main():
outFile = open("total.txt","a") #outFile is the output txt file
number = int(input("Enter a number: ")) # Ask for a number
if number != 0: # if number is 0 -> do nothing
outFile.write(str(number)+"\n") # str(number) converts it to string
total = number # initialize total to number
while(number != 0): # keep looping while number != 0, as if 0 then terminate
number = int(input("Enter a number: "))
if number != 0:
total = total_numbers(number, total) # call totak_numbers to add number to total
outFile.write(str(number)+" , "+str(total)+"\n") # write both value to outFile comma seperated
outFile.close() # close file
def total_numbers(number, total):
total = total + number # total += number
return total
main()
Get Answers For Free
Most questions answered within 1 hours.