我是python的新手。我也是这个主题的初学者。所以任何帮助都会很棒!
我试图编写一个代码来计算3 ^ 2 = 9,2 ^ 3 = 8等问题的答案。
我知道这有**。但我需要使用while循环和for循环。
我不知道我做错了什么,以及我对这些循环的需求。如果他们输入的数字小于零,我还需要循环告诉用户错误的答案。如果输入错误的答案,我需要知道如何从头开始循环。任何帮助将不胜感激!
这是我到目前为止所做的:
base1 = int(input("Base:"))
base2 = 1
exponent = int(input("Exponent:"))
ExpNeeded = True
while ExpNeeded:
for hat in range(exponent):
base2 = base2 * base1
print("Answer:" , base2)
ExpNeeded = False
else:
print("Please enter a number greater than zero")
答案 0 :(得分:0)
我使用了您的代码,您可以使用while
而不使用ExpNeeded和break
循环(while
),即while True:
ExpNeeded = True
while ExpNeeded :
base2 = 1
base1=int(input("Base:"))
while base1<=0:
print "Please enter a number greater than zero"
base1=int(input("Base:"))
exponent=int(input("Exponent:"))
while exponent<=0:
print "Please enter a number greater than zero"
exponent=int(input("exponent:"))
for hat in range(exponent):
base2 = base2 * base1
print 'answer: ' , base2
print "to repeat press 1 else press 0"
if int(input(" "))==1:
pass
else:
ExpNeeded =False
输出:
Base:2
Exponent:5
answer: 32
to repeat press 1 else press 0
1
Base:-2
Please enter a number greater than zero
Base:6
Exponent:2
answer: 36
to repeat press 1 else press 0
0