For 循环 - 我只能输入 1 个整数
For loop - I can only enter 1 integer
我无法输入一个以上的整数。我应该能够指定多少,然后像这样输入...
How many integers? 3
Please enter an integer 1: 5
Please enter an integer 2: 2
Please enter an integer 3: 6
Using a for loop
5
2
6
但是我只被要求输入整数 1(在连续循环中)
代码如下:-
#!/usr/bin/env python2
import sys
target_int = raw_input("How many integers?")
try:
target_int = int(target_int)
except ValueError:
sys.exit("You must enter an integer")
ints = list()
count = 0
while count < target_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
print("Using a for loop")
for value in ints:
print(str(value))
~
您的主要问题如前所述,您没有设置 isint
。但是无论如何您都不应该,因为您已经在 try
块中进行了投射。所以如果你到达这个代码,它必然是一个int
。 2其他小东西是,我是运行在Python 3,因为是14年前发布的,所以raw_input
是input
,好进使用 []
而不是 list()
初始化列表的习惯。它有时更快,永远不会更慢。
这是工作代码:
import sys
target_int = input("How many integers?")
try:
target_int = int(target_int)
except ValueError:
sys.exit("You must enter an integer")
ints = []
count = 0
while count < target_int:
new_int = input("Please enter integer {0}:".format(count + 1))
try:
new_int = int(new_int)
except ValueError:
print("You must enter an integer")
continue # so it doesn't try to append
# No need to check isint. It is.
ints.append(new_int)
count += 1
print("Using a for loop")
for value in ints:
print(str(value))
另一个小的改进是完全避免 count
,因为它总是已经是 ints
的长度。
import sys
target_int = input("How many integers?")
try:
target_int = int(target_int)
except ValueError:
sys.exit("You must enter an integer")
ints = []
while len(ints) < target_int:
new_int = input("Please enter integer {0}:".format(len(ints) + 1))
try:
ints.append(int(new_int))
except ValueError:
print("You must enter an integer")
print("Using a for loop")
for value in ints:
print(str(value))
我无法输入一个以上的整数。我应该能够指定多少,然后像这样输入...
How many integers? 3
Please enter an integer 1: 5
Please enter an integer 2: 2
Please enter an integer 3: 6
Using a for loop
5
2
6
但是我只被要求输入整数 1(在连续循环中)
代码如下:-
#!/usr/bin/env python2
import sys
target_int = raw_input("How many integers?")
try:
target_int = int(target_int)
except ValueError:
sys.exit("You must enter an integer")
ints = list()
count = 0
while count < target_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
print("Using a for loop")
for value in ints:
print(str(value))
~
您的主要问题如前所述,您没有设置 isint
。但是无论如何您都不应该,因为您已经在 try
块中进行了投射。所以如果你到达这个代码,它必然是一个int
。 2其他小东西是,我是运行在Python 3,因为是14年前发布的,所以raw_input
是input
,好进使用 []
而不是 list()
初始化列表的习惯。它有时更快,永远不会更慢。
这是工作代码:
import sys
target_int = input("How many integers?")
try:
target_int = int(target_int)
except ValueError:
sys.exit("You must enter an integer")
ints = []
count = 0
while count < target_int:
new_int = input("Please enter integer {0}:".format(count + 1))
try:
new_int = int(new_int)
except ValueError:
print("You must enter an integer")
continue # so it doesn't try to append
# No need to check isint. It is.
ints.append(new_int)
count += 1
print("Using a for loop")
for value in ints:
print(str(value))
另一个小的改进是完全避免 count
,因为它总是已经是 ints
的长度。
import sys
target_int = input("How many integers?")
try:
target_int = int(target_int)
except ValueError:
sys.exit("You must enter an integer")
ints = []
while len(ints) < target_int:
new_int = input("Please enter integer {0}:".format(len(ints) + 1))
try:
ints.append(int(new_int))
except ValueError:
print("You must enter an integer")
print("Using a for loop")
for value in ints:
print(str(value))