简体   繁体   中英

using function and random numbers

I have a question:

When the program runs, it will randomly choose a number between 1 and 6. (Or whatever other integer you prefer — the number of sides on the die is up to you.) The program will print what that number is. It should then ask you if you'd like to roll again. For this project, you'll need to set the min and max number that your dice can produce. For the average die, that means a minimum of 1 and a maximum of 6. You'll also want a function that randomly grabs a number within that range and prints it.

This is what I have done so far:

import random

x = random.randint(1,6)

print("You roll a die ", x)
new_try = input("\n\n Do you want to roll a die again?")

if str(new_try) == 'yes':

    print("You roll a die ", x)

else:

    print("Cool game!")

I am still getting same numbers :(

我认为您可以做的是每次运行新尝试时设置不同的种子。

You aren't changing x the second time, and merely printing it out again, giving the same result. Here is the code for a fixed version:

import random

x = random.randint(1, 6)

print("You roll a die", x)

new_try = input("\n\n Do you want to roll again? ")

if new_try == 'yes':
    x = random.randint(1, 6)
    print("You roll a die", x)
else:
    print("Cool game!")

If you want to use a function for it, you can do it over multiple times:

import random

def roll_dice():
    x = random.randint(1, 6)
    print("Dice was rolled: " + str(x))
    try_again = input("Do you want to try again? ")
    if try_again == 'yes':
        roll_dice()

roll_dice()

x is not the diceroll, that is random.randint(1,6) . So after x = random.randint(1,6) , x stores the result of a single diceroll which happened earlier, and available to provide that single result any time in the future. So x stores a number, not the fact that it should be generated randomly.

If you want a function for rolling a dice, that would be def for first attempts:

def diceroll():
  return random.randint(1,6)

having this function, any subsequent print(diceroll()) (note that it is a function call, diceroll() ) would print the result of a different roll (and results could be equal only by coincidence). Or, you could again store the result of a single diceroll as x=diceroll() , so it could be re-used multiple times in the future (let's say you want to compare it to the user's guess and also print it)

Side note: technically you can store functions in variables too, x=diceroll would store the function, so x would not be a number, but the act of rolling the dice, and would have to be called as a function, like print(x()) .

If you want to produce different numbers at different times, you have to use a seed value. Here is an example to explain:

import random
random.seed( 3 )
print "Random number with seed 3 :", random.random() #will generate a random number 
#if you want to use the same random number once again in your program
random.seed( 3 )
random.random()

I will not make the program for you. I have explained you the concept. Now just implement it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM