Python Code-While 循环永无止境 [英] Python Code-While loop never end
本文介绍了Python Code-While 循环永无止境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Python 新手.正在尝试学习它.这是我的代码:
I am new to Python.Trying to learn it. This is my Code:
import sys
my_int=raw_input("How many integers?")
try:
my_int=int(my_int)
except ValueError:
("You must enter an integer")
ints=list()
count=0
while count<my_int:
new_int=raw_input("Please enter integer{0}:".format(count+1))
isint=False
try:
new_int=int(new_int)
except:
print("You must enter an integer")
if isint==True:
ints.append(new_int)
count+=1
代码正在执行但循环总是重复并且不允许我输入第二个整数.
The code is executing but the loop is always repeating and is not allowing me to enter 2nd integer.
输出:
How many integers?3
Please enter integer1:1
Please enter integer1:2
Please enter integer1:3
Please enter integer1:
我能知道我的代码有什么问题吗?谢谢
Can i know what is wrong with my code? Thank you
推荐答案
你的代码的问题是 isint
永远不会改变并且总是 False
,因此 count
永远不会改变.我猜你的意图是当输入是一个有效的整数时,增加 count
;否则,对 count
不做任何事情.
The problem of your code is that isint
is never changed and is always False
, thus count
is never changed. I guess your intention is that when the input is a valid integer, increase the count
;otherwise, do nothing to count
.
这里是代码,不需要isint
标志:
Here is the code, isint
flag is not need:
import sys
while True:
my_int=raw_input("How many integers?")
try:
my_int=int(my_int)
break
except ValueError:
print("You must enter an integer")
ints=list()
count=0
while count<my_int:
new_int=raw_input("Please enter integer{0}:".format(count+1))
try:
new_int=int(new_int)
ints.append(new_int)
count += 1
except:
print("You must enter an integer")
这篇关于Python Code-While 循环永无止境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文