2017-02-11 33 views
2

试图做一个布尔测试循环,其中用户需要输入10到1000之间的数字,包括10和1000,他们需要留在这循环,直到他们输入正确的数字,然后我会完成else语句。我想返回一个“二重奏”列表中的顶级元素的数量

我尝试这样做:

while (num_years < 10) and (num_years > 1000): # boolean test , note == (= will give you an error!) 
    print("Your input must be 10 and 1000") 
    input("Enter number of years to simulate (currently " + str(num_years) + "): ") 
else: 
    print() 

这:

while (num_years == range(10, 1000)): # boolean test , note == (= will give you an error!) 
    print("Your input must be 10 and 1000") 
    input("Enter number of years to simulate (currently " + str(num_years) + "): ") 
else: 
    print() 
+1

OFFTOPIC评论:从中我记得你永远不会将你的问题标记为答案,请参阅[this](http://stackoverflow.com/help/someone-answers)链接。 – MaLiN2223

+1

提示*“您的输入必须是10和1000”*没有意义(一个数字不能(http://www.dictionary.com/browse/be)有两个不同的数字......此外,即使您添加**之间**这个词在适当的位置上,如果你想要**包含**,(10个是允许的1000个)或**独占**,(10个是1000个是不允许的),你仍然应该澄清。如果你在源自他们课程的python问题标记为你的python问题的答案,那么你在python的作者上的在线课程将会很感激,因为他们中的大多数是(http://stackoverflow.com/users/7443013/supernova):) – shash678

+0

between并包括 – Supernova

回答

0

好吧,首先:

(num_years < 10) and (num_years > 1000) 

这始终是假,因为一个数不能少于10同时超过1000个。

的所有

num_years == range(10, 1000) 

二行不通,因为你必须在右边的左边和generator(可能)的整数。

但是你可以尝试这样的事情:

possible_years = range(10,101) 
num_years = 50 
while num_years in possible_years: # or 9 < num_years < 100 
    print("Your input must be 10 and 1000") 
    entered_years = input("Enter number of years to simulate (currently " + str(num_years) + "): ") 
    num_years = int(entered_years) # remeber to check if entered_years is int before this line! 
else: 
    print() 

但是仍有追赶,你需要检查,如果用户输入一个整数。

+0

它没有工作,我把它放在10,它通过循环, – Supernova

+0

@Supernova因为你排除了1 0,范围为'num_years <10'。我编辑了我的答案以满足您的需求。 – MaLiN2223

+1

'list(range(...))'不是必须的'range()'支持'in'运算符并且更具有内存效率。 50范围内(10,1001)== True。注意:范围是'range(10,1001)': – AChampion

0

你永远不会改变num_years的价值,所以条件永远不会改变。修复您的布尔逻辑:

num_years = 0 
while (num_years < 10) or (num_years > 1000): 
    print("Your input must be 10 and 1000") 
    num_years = int(input("Enter number of years to simulate (currently {}): ".format(num_years))) 
else: 
    print() 

然而,这种假设有人正在进入一个号码,如果你需要覆盖一个非数字值,则使用try: except: pass块:

try: 
    num_years = int(input("Enter number of years to simulate (currently {}): ".format(num_years))) 
except ValueError: 
    pass 

注:=是分配不平等测试。使用in来测试值是否在listrange中。

0

你需要做的是一个while循环来检查输入是否有效。如果不是,它会再次循环。你也从来没有把输入分配给一个变量。

n = None 
while n is None or not 10 <= n <= 1000: 

    if n is not None: # Only happens if it's not the first input 
     print('Invalid number, please try again') 

    try: 
     n = int(input('Enter number of years to simulate (10-1000): ')) # Convert the 
                      # string to an int 
    except: 
     pass 

print('Simulating {} years...'.format(n)) 

它将继续说一次模拟一个有效的数字输入,因为

not 10 <= n <= 1000 

将评估为false。

您可以在此链接试试:https://repl.it/FftK/0

+0

当我输入范围内的数字例如15它仍然说输入10到1000之间的数字 – Supernova

+0

你在什么Python版本?我在3.6,并按预期工作。看看repl链接,它在那里工作。 –

+0

几乎都有它,它会检查拳头时间,但不是第二个: – Supernova

1

你可以尝试这样的事情,因为它如果用户输入一个有效的数字还检查:

while True: 
    number = input("Enter number of years to simulate (10-1000): ") 
    try: 
    years = int(number) 
    if years >= 10 and years <= 1000: 
     break 
    else: 
     print("Your input must be between 10 and 1000 inclusive") 
    except ValueError: 
    print("That's not an int!") 

print("Simulating %d years..." % years) 

实例应用:

Enter number of years to simulate (10-1000): asd 
That's not an int! 
Enter number of years to simulate (10-1000): 5 
Your input must be between 10 and 1000 inclusive 
Enter number of years to simulate (10-1000): 245 
Simulating 245 years... 

试试吧here!

+1

这似乎是最好的答案(不是问题存在,而是评论)。 +1为你先生,因为我从来不会猜到这是OP要求的。 – MaLiN2223

0

N =无 而n是无或不10 < = N < = 1000:

if n is not None: # Only happens if it's not the first input 
    print('Invalid number, please try again') 

try: 
    n = int(input('Enter number of years to simulate (10-1000): ')) # Convert the 
                     # string to an int 
except: 
    pass 

打印( '模拟{}年......' 制(n))。

相关问题