我刚开始为学校编程,这个作业要求我输入一个数字,然后用它从0到数字计数,然后选择数学运算,然后使用数学运算。
当我输入一个负数时会出现问题,它会给我正确的信息“太小 - 再试一次:”。但是一旦我输入一个正数,它仍然会将我原来的负数分配给该程序。
这是我的noobie代码:
num1 = int(input("\nEnter a number 1 or greater:\t"))
counting = num1 + 1
def count():
print("Counting from 0 to", num1,":")
for i in range(0,counting):
print(i, end = ' ')
math_op()
def reset():
num1 = int(input("Too small - Try again: "))
if num1 <= 0:
reset()
else:
count()
def math_op():
ops = input("\n\nChoose math operation (+, -, *)")
if ops in ('+'):
print("Table for",num1,"using +:")
for i in range(1,11):
print(num1 ,'+', i ,'=', num1 + i)
if ops in ('-'):
print("Table for",num1,"using -:")
for i in range(1,11):
print(num1 ,'-', i ,'=', num1 - i)
if ops in ('*'):
print("Table for",num1,"using *:")
for i in range(1,11):
print(num1 ,'*', i ,'=', num1 * i)
if num1 <= 0:
reset()
else:
count()
答案 0 :(得分:0)
是的,你错过了while
循环:
num1 = -1
while num1 < 1:
num1 = int(input("\nEnter a number 1 or greater:\t"))
# remaining code after here
答案 1 :(得分:0)
当校正为正时,您从未设置要计数的数字:
def reset():
num1 = int(input("Too small - Try again: "))
if num1 <= 0:
reset()
else:
counting = num1 + 1
count()