Python 我想返回a";中顶级元素的数量;“duo”;列表

Python 我想返回a";中顶级元素的数量;“duo”;列表,python,while-loop,boolean,Python,While Loop,Boolean,尝试执行一个布尔测试循环,其中用户需要输入一个介于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")

尝试执行一个布尔测试循环,其中用户需要输入一个介于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()
好的,首先:

(num_years < 10) and (num_years > 1000)
将永远不会工作,因为您(可能)在左边和右边都有整数

但是,您可以尝试类似的方法:

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()
可能的年数=范围(10101)
年数=50
而可能的年数中的年数:或9<年数<100
打印(“您的输入必须是10和1000”)
输入的年数=输入(“输入要模拟的年数(当前“+str(num_years)+”):)
num_years=int(输入的_years)#请记住检查输入的_years在此行之前是否为int!
其他:
打印()

但是仍然存在一个问题,您需要检查用户是否输入了整数。

您永远不会更改
num\u 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()

注:
=
不是平等测试。使用
中的
测试值是否在
列表中
范围中

需要做的是执行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) )
n=None

当n为None或不是10时,您可以尝试类似的操作,因为它还会检查用户输入的数字是否有效:

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)
试试看

n=None
虽然n是无或不是10,但主题注释:我记得您从未将问题标记为已回答,请参见链接。提示“您的输入必须是10和1000”没有意义(一个数字不能是两个不同的数字…此外,即使您在适当的位置添加单词between,您仍应澄清是否需要包含,(允许使用10个1000个)或独占(不允许使用10个1000个)。最后,我认为,如果您将来自python作者课程的python问题标记为已回答,您将不胜感激,因为大多数问题:)这是我最初的问题:试图做一个布尔测试循环,用户需要输入一个介于10和1000之间的数字,包括10和1000,它不起作用,我输入了10,它就直接通过了循环,@Supernova,因为你从范围
num_years
列表(范围(…)
没有必要
range()
支持
运算符中的
,并且内存效率更高,例如范围(101001)中的
50==True
。注意:范围是
范围(101001)
:@AChampion,这是非常正确的。谢谢你的提示。如果我输入10,它仍然没有验证,而是告诉我输入范围:当我在范围内输入一个数字时,例如15,它仍然说输入一个介于10和1000之间的数字。你使用的是什么python版本?我使用的是3.6,它按预期工作。看看repl链接,它在那里工作。几乎有e它第一次检查,但不是第二次:虽然num_years>=10和num_years,但它对我的迭代次数很好……你看过repl链接了吗?这似乎是最好的答案(不是问题本身,而是评论)。+1给你一个,先生,因为我从来没有猜到OP是在问这个问题。
try:
    num_years = int(input("Enter number of years to simulate (currently {}): ".format(num_years)))
except ValueError:
    pass
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
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...
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