'while' 当输入上面的代码以清除某些输入时循环运行
'while' loop runs when code entered above to weed out certain inputs
我不明白为什么我的代码没有清除任何浮点数并让它们 运行 在 while 循环中(然后搞砸了代码)。有什么我可以做的来保护 'while loop' - 只要在输入中输入浮点数就不要 运行 吗?
这是我的代码:
if not a.isdigit() and int(a) < 0 and not b.isdigit() and int(b) < 0 :
print("Invalid input")
else :
count = 1
while count < int(b) :
c = count * int(a)
print('{} * {} = {}'.format(count, a, c))
count = count + 1
c = int(a) * int(b)
print('{} * {} = {}'.format(b, a, c))
预先检查 b 是否有小数:
if round(b)==b:
您可能需要将这些 and
更改为 or
,以便任何这些条件(a.isdigit()
、int(a) < 0
等)为真都会导致避免循环。
我不明白为什么我的代码没有清除任何浮点数并让它们 运行 在 while 循环中(然后搞砸了代码)。有什么我可以做的来保护 'while loop' - 只要在输入中输入浮点数就不要 运行 吗?
这是我的代码:
if not a.isdigit() and int(a) < 0 and not b.isdigit() and int(b) < 0 :
print("Invalid input")
else :
count = 1
while count < int(b) :
c = count * int(a)
print('{} * {} = {}'.format(count, a, c))
count = count + 1
c = int(a) * int(b)
print('{} * {} = {}'.format(b, a, c))
预先检查 b 是否有小数:
if round(b)==b:
您可能需要将这些 and
更改为 or
,以便任何这些条件(a.isdigit()
、int(a) < 0
等)为真都会导致避免循环。