gpt4 book ai didi

Python 随机数生成器错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:37 25 4
gpt4 key购买 nike

你好,我刚开始用 Python 编程,我只是想制作一个 RNG,但有一些我不明白的错误。

这是代码

import random
import time

numbersGenerated = 0

print('Hello! How many numbers do you want?')
numbersNeeded = input()
print('What would you like the minimum for the numbers be?')
Min = input()
print('And the maximum?')
Max = input()

numbersNeeded = str(numbersNeeded)
numbersGenerated = str(numbersGenerated)

while numbersGenerated < numbersNeeded:
number = random.randint(Min, Max)
number = int(number)

numbersGenerated = numbersGenerated + 1

print(number)
print()
time.sleep(2)

print('All done. Hope to see you again!')

回溯错误说

File "C:/Python32/Number_Generator.py", line 20, in <module>
number = random.randint(Min, Max)
File "C:\Python32\lib\random.py", line 215, in randint
return self.randrange(a, b+1)
TypeError: Can't convert 'int' object to str implicitly

我也更改了第 13 行和 int,但这并没有改变结果。

最佳答案

  1. 你正在做 while numbersGenerated < numbersNeeded什么时候numbersGeneratednumbersNeeded是字符串,这可能不是你想要的。

  2. 您正在调用 random.randint(Min, Max)同时MinMax是字符串,它们应该是整数。

  3. 不需要 number = int(number) ,它已经是一个整数了。

  4. numbersGenerated = numbersGenerated + 1同样,变量是一个字符串

关于Python 随机数生成器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9729374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com