-1

alright, So I'm working on a small python Command-line calculator,

this was just a test:

import time

calc = print("\n what calculation do you want to use\n")

time.sleep(1)

menu = print(" MENU:\n **********\n mult for multiplication\n add for addition\n sub for substraction\n div for divide\n **********")

time.sleep(1)

ask = input(" what calculation do you want to use from the menu? \n")

much = input(" how many numbers do you calculate? (5 maximumn & 2 minimum) \n")

if(much == " 2" or much == "2" and ask == " mult" or ask == "mult"):
    num1_2 = input(" First Number? ")
    num2_2 = input("\n Second Number? ")
    answer_2 = print(int(num1_2) * int(num2_2))


time.sleep(11)

and it was working correctly, i was planning to complete all the if: and the elif: statements, but then i thought that i could make my code more efficient, so i thought i can use a for loop like this:

for value in range(int(much)):
    output = input("Number? ")

print(int(value) * int(value))

note: (much) is the the variable name that i used for declaring how many numbers does the user wants to calculate

so, when i used the for loop it showed a wrong answer, for example when i multiplied 2 by 2 the answer was 1.

so how can i declare multiple items in the for loop to multiplie the first user input by the second one?

this is confusing because the number of outputs is not stable.

1 Answers1

0

Do know that, the for loop remembers only the value of the last iteration. You may have a variable(Not necessarily a list) outside of store the values of your iteration result. For example:

result = 1
for value in range(int(much)):
    output = input("Number? ")
    result*=int(output)
print(result)
Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18
  • Yes that worked but i didn't quite understand what do you mean by (You may have a variable(Not necessarily a list) outside of store the values of your iteration result)? – Dev Turaani Aug 04 '20 at 00:10
  • I think @redmicelles was saying that you can use an int or float (such as *result* in the example given), instead of a list as suggested by alaniwi, to store the intermediate and final computations. – Joe Aug 04 '20 at 04:10