Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在输入中找到随机数?_Python_Python 3.x - Fatal编程技术网

Python 如何在输入中找到随机数?

Python 如何在输入中找到随机数?,python,python-3.x,Python,Python 3.x,我希望print语句打印介于下限和上限之间的数字 我不断得到错误代码: Traceback (most recent call last):File "python", line 5, in <module> ValueError: non-integer arg 1 for randrange() input()函数始终返回字符串。如果要使用整数,如本例中所示,则必须使用int()将这些字符串转换为整数。但是,如果用户输入了无法转换为整数的内容(例如“hi”,或只是点击retur

我希望print语句打印介于下限和上限之间的数字

我不断得到错误代码:

Traceback (most recent call last):File "python", line 5, in <module> ValueError: non-integer arg 1 for randrange()

input()
函数始终返回字符串。如果要使用整数,如本例中所示,则必须使用
int()
将这些字符串转换为整数。但是,如果用户输入了无法转换为整数的内容(例如“hi”,或只是点击return),则在尝试转换时会出现错误。为了解决这个问题,您需要研究try和except语句。希望有帮助

函数的
input()
总是返回一个字符串。如果要使用整数,如本例中所示,则必须使用
int()
将这些字符串转换为整数。但是,如果用户输入了无法转换为整数的内容(例如“hi”,或只是点击return),则在尝试转换时会出现错误。为了解决这个问题,您需要研究try和except语句。希望有帮助

试试这个:

在这里,直到你输入一个数字,它才会停止。
int
以外的输入将被视为无效输入

代码中的错误在于,从输入读取的所有内容都被视为字符串

from random import*
while True:
        try:
            lowRange = int(input('What is the lower range number?'))
            break
        except:
            print("That's not a valid input!")
while True:
        try:
            hiRange = int(input('What is the higher range nunmber?'))
            break
        except:
            print("That's not a valid input!")

ran = randrange (lowRange,hiRange)
print (ran)

试试这个:

在这里,直到你输入一个数字,它才会停止。
int
以外的输入将被视为无效输入

代码中的错误在于,从输入读取的所有内容都被视为字符串

from random import*
while True:
        try:
            lowRange = int(input('What is the lower range number?'))
            break
        except:
            print("That's not a valid input!")
while True:
        try:
            hiRange = int(input('What is the higher range nunmber?'))
            break
        except:
            print("That's not a valid input!")

ran = randrange (lowRange,hiRange)
print (ran)

下限和上限是字符串,通过
int(下限)
int(上限)
下限和上限是字符串,通过
int(下限)
int(上限)