anything interesting 6.8. Do addition Summary Now you will replace the pass stat
ID: 3850732 • Letter: A
Question
anything interesting 6.8. Do addition Summary Now you will replace the pass statement inside the addition case of the iflelif ladder with something that actually does addition. Since the only number we have right now is the accumulator, you will need to ask the user for a number This number will be added to the value of the accumulator and then stored back into the accumulator. Do this 1. the in your iflelif ladder where choice 1 is handled this is for addition). 2. You can leave the comment where it is. 3. Delete the pass statement. 4. Get a number from the user, prompting with "Enter a number: Convert the number to a float and store it in a variable. Name the variable something appropriate. Do not use the accumulator variable for this that variable already contains an important number. 5. Add the number in that variable to the accumulator variable and store the sum back into the accumulator variable. 6. Test your program. What do you expect should happen now when you run your program? Make sure it works. 6.9. Do subtraction write the next case in the iflelse ladder so that it is similar to the first case. Get a number from the user (a float, subtract it from the accumulator and store it b into the accumulator. Test it.Explanation / Answer
NOTE: Your code seem to be correct with some duplicate code like you are asking about option twice. But as per your assignment i think you need to include other steps of doing addition, subtraction etc which i have finished here. Please check and let me know if you have any questions.
Code:
#!/usr/local/bin/python3
from math import sqrt
def main():
question = 'What is your option? '
contin = True
opt = -1
accum = 0.0
while contin:
print("Accumulator=",accum)
print("Please Choose...")
print("1)Addition")
print("2)Subtraction")
print("3)Multiplication")
print("4)Division")
print("5)Square root")
print("6)Clear")
print("0)Exit")
opt = int(input(question))
if opt == 0:
contin = False
elif opt == 1:
num = float(input("Enter a number? "))
accum += num
elif opt == 2:
num = float(input("Enter a number? "))
accum -= num
elif opt == 3:
num = float(input("Enter a number? "))
accum *= num
elif opt == 4:
num = float(input("Enter a number? "))
accum /= num
elif opt == 5:
num = float(input("Enter a number? "))
accum = sqrt(num)
elif opt == 6:
print("Clear")
pass
else:
print("Wrong option. Try again...")
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python3 arithmetic.py
Accumulator= 0.0
Please Choose...
1)Addition
2)Subtraction
3)Multiplication
4)Division
5)Square root
6)Clear
0)Exit
What is your option? 1
Enter a number? 100
Accumulator= 100.0
Please Choose...
1)Addition
2)Subtraction
3)Multiplication
4)Division
5)Square root
6)Clear
0)Exit
What is your option? 2
Enter a number? 20
Accumulator= 80.0
Please Choose...
1)Addition
2)Subtraction
3)Multiplication
4)Division
5)Square root
6)Clear
0)Exit
What is your option? 3
Enter a number? 10
Accumulator= 800.0
Please Choose...
1)Addition
2)Subtraction
3)Multiplication
4)Division
5)Square root
6)Clear
0)Exit
What is your option? 4
Enter a number? 10
Accumulator= 80.0
Please Choose...
1)Addition
2)Subtraction
3)Multiplication
4)Division
5)Square root
6)Clear
0)Exit
What is your option? 5
Enter a number? 900
Accumulator= 30.0
Please Choose...
1)Addition
2)Subtraction
3)Multiplication
4)Division
5)Square root
6)Clear
0)Exit
What is your option? 6
Clear
Accumulator= 30.0
Please Choose...
1)Addition
2)Subtraction
3)Multiplication
4)Division
5)Square root
6)Clear
0)Exit
What is your option? 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.