简体   繁体   中英

How do i declare multiple items in a for loop in python

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM