【问题标题】:Number printer, Random integer not working数字打印机,随机整数不起作用
【发布时间】:2021-03-29 04:47:38
【问题描述】:

好的,所以我正在尝试在 repl.it 上创建一个程序,它可以打印偶数、奇数、连续数或随机数,您可以在其中决定所需的时间、打印量和(随机)最高和最低的数字。

但是,每当我选择随机并尝试运行它时。弹出这个

  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()

【问题讨论】:

标签: python random syntax-error


【解决方案1】:

正如“@Johnny Mopp”所说,

翻转参数,使较小的数字在前:print(random.randint(int(randlowcap),int(randhighcap)),flush=True, end="")。 randint() 返回一个随机整数 N,使得 a

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 2020-07-24
    • 2011-07-28
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2014-05-12
    • 2011-01-19
    相关资源
    最近更新 更多