gpt4 book ai didi

python - 数字打印机,随机整数不起作用

转载 作者:行者123 更新时间:2023-12-03 08:32:44 24 4
gpt4 key购买 nike

好吧,所以我试图在repl.it上创建一个程序,该程序可以打印偶数,奇数,连续或随机数,您可以在其中确定所需的时间,打印量以及(对于随机数)最高和最低数。
但是,无论何时我选择随机并尝试运行它。这 pop

  File "main.py", line 76, in <module>
massprint()
File "main.py", line 24, in massprint
print(random.randint(int(randhighcap),int(randlowcap)),flush=True, end=" ")
File "/usr/lib/python3.8/random.py", line 248, in randint
return self.randrange(a, b+1)
File "/usr/lib/python3.8/random.py", line 226, in randrange
raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (100, 1, -99)
这是repl项目的链接,因此您可以查看代码。 (或者代码在下面。)
https://repl.it/@MichaelGordon1/OddEvenConsecutive-Number-printer#main.py
import time
import random
from random import randint
import sys
def massprint():
againagain = int(times)
even=0
odd=1
consec=0
rand=0
while againagain >= 0:
time.sleep(float(speed))
againagain -= 1
if eocr=="e":
print(even,flush=True, end=" ")
even=even+2
elif eocr=="o":
print(odd,flush=True, end=" ")
odd=odd+2
elif eocr=="c":
print(consec,flush=True, end=" ")
consec=consec+1
elif eocr=="r":
print(random.randint(int(randhighcap),int(randlowcap)),flush=True, end=" ")
else:
print("Unrecognized. Type either e, o or c")

if againagain<=1:
print(" \n")
print("Do you want to print even numbers, odd numbers \n, consecutive or random numbers? ")
eocr=input("e/o/c/r ")
time.sleep(1)
times=input("And how many times should it do this? ")
time.sleep(1)
speed=input("Okay, And What speed should it print the numbers, \nAnswer in seconds. (0.02 Is Default) ")
time.sleep(1)
againagain = int(times)
if eocr=="e":
print("Alright, The final number will be",str(againagain * 2))
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
massprint()
time.sleep(1)
print("Done!")
else:
exit()
elif eocr=="o":
print("Alright, The final number will be",str(againagain * 2+1))
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
massprint()
time.sleep(1)
print("Done!")
else:
exit()
elif eocr=="c":
print("Alright, The final number will be",str(againagain))
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
massprint()
time.sleep(1)
print("Done!")
elif eocr=="r":
time.sleep(1)
randhighcap=input("And what do you want the highest random number to be? ")
time.sleep(1)
randlowcap=input("And what do you want the lowest random number to be? ")
time.sleep(1)
print("Alright, It will print",str(againagain),"random numbers \nWith a high cap of",randhighcap,"\nAnd a low cap of",randlowcap)
varcontinue=input("Is this okay? (y/n) ")
if varcontinue=="y":
print(" ")
massprint()
time.sleep(1)
print("Done!")
else:
exit()
else:
exit()

最佳答案

正如“@Johnny Mopp”所说,

Flip the parameters so the lower number comes first: print(random.randint(int(randlowcap),int(randhighcap)),flush=True, end=" "). randint() Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).


老实说,我不敢相信我没有想到这一点。

关于python - 数字打印机,随机整数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65358598/

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