在python中更改随机数

时间:2018-04-02 13:32:55

标签: python python-3.x random

import random

def guess_number_game():

number = random.randint(1, 101)

points = 0

print('You already have ' + str(points) + ' point(s)')

playing = True

while playing:


    guess = int(input('Guess the number between 1 and 100: '))

    if guess > number:
        print('lower')
    elif guess < number:
        print('Higher')
    else:
        print('You got it, Good job!!')
        playing = False
        points += 1
        play_again = True

    while play_again:

        again = input('Do you want to play again type yes/no: ')

        if again == 'yes':
            playing = True
            play_again = False
        elif again == 'no':
            play_again = False
        else:
            print('please type yes or no')
print('Now you have ' + str(points) + ' point(s)')




guess_number_game()

我刚开始学习python,我做了这个简单的猜数游戏,但是 如果你再试一次,你会得到相同的号码。

e.g。这个数字是78而且你猜对了,但是你想再玩一次,所以你说你想再玩一次,这个数字仍然是78。

所以如何制作它以便每当有人玩游戏时数字都会改变

3 个答案:

答案 0 :(得分:1)

您需要将数字设置为循环中随机生成的数字。

示例:

import random


def guess_number_game():

   playing = True

   while playing:

      number = random.randint(1, 101)

      points = 0

      print('You already have ' + str(points) + ' point(s)')

      playing = True
      print(number)

      guess = int(input('Guess the number between 1 and 100: '))

      if guess > number:
          print('lower')
      elif guess < number:
          print('Higher')
      else:
          print('You got it, Good job!!')
          playing = False
          points += 1
          play_again = True

      while play_again:

          again = input('Do you want to play again type yes/no: ')

          if again == 'yes':
              playing = True
              play_again = False
          elif again == 'no':
              play_again = False
          else:
              print('please type yes or no')
  print('Now you have ' + str(points) + ' point(s)')




guess_number_game()

答案 1 :(得分:0)

你在游戏之外定义你的随机数,当有人决定再玩一次时设置一个新的随机数!

import random

def guess_number_game():

    number = randbelow(1, 101)

    points = 0

    print('You already have ' + str(points) + ' point(s)')

    playing = True

    while playing:

        print(number)

        guess = int(input('Guess the number between 1 and 100: '))

        if guess > number:
            print('lower')
        elif guess < number:
            print('Higher')
        else:
            print('You got it, Good job!!')
            playing = False
            points += 1
            play_again = True

        while play_again:

            again = input('Do you want to play again type yes/no: ')

            if again == 'yes':
                playing = True
                play_again = False
                number = randbelow(1, 101)
            elif again == 'no':
                play_again = False
            else:
                print('please type yes or no')
    print('Now you have ' + str(points) + ' point(s)')

guess_number_game()

答案 2 :(得分:0)

import random

def guess_number_game():
    points = 0

    print('You already have ' + str(points) + ' point(s)')

    playing = True
    play_again=False

    number = random.randint(1, 101)
    #print(number)

    while playing:

        guess = int(input('Guess the number between 1 and 100: '))

        if guess > number:
            print('lower')
        elif guess < number:
            print('Higher')
        else:
            print('You got it, Good job!!')
            number = random.randint(1, 101)
            points += 1

            again = input('Do you want to play again type yes/no: ')

            if again == 'yes':
                playing = True
            elif again == 'no':
                playing = False

    print('Now you have ' + str(points) + ' point(s)')




guess_number_game()