random.randomint 有变量

random.randomint with variables

好的,所以我正在使用用户定义的参数制作一个 python 随机数生成器。我在 python 3.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' 作为参数。你有什么想法?

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