随机.randomint带变量

2024-04-26 14:57:12 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,我正在用用户定义的参数制作一个python随机数生成器。我在python3.4上写这个。代码如下:

import random
import time
name = input("whats your name? ")
numOfNums = input("ok " + name + " how many numbers do you want to randomize? ")

def randomGenerator():
    randomFinal = random.randint(1, numOfNums)
    print (randomFinal)
    rerun = input("Do you want to pick another random number? ")
    if rerun == "yes":
        time.sleep(0.05)
        randomGenerator()
    else:
        print("you may now close this windows")

randomGenerator()

我对线路有问题:

randomFinal = random.randint(1, numOfNums)

我不能使用变量'numOfNums'作为参数。你有什么想法吗?你知道吗


Tags: tonameimportrerunyouinput参数time
1条回答
网友
1楼 · 发布于 2024-04-26 14:57:12

使用int()将其设为整数:

import random
import time
name = input("whats your name? ")
numOfNums = int(input("ok " + name + " how many numbers do you want to randomize? "))

def randomGenerator():
    randomFinal = random.randint(1, numOfNums)
    print (randomFinal)
    rerun = input("Do you want to pick another random number? ")
    if rerun == "yes":
        time.sleep(0.05)
        randomGenerator()
    else:
        print("you may now close this windows")

randomGenerator()

相关问题 更多 >