繁体   English   中英

在python中更改随机数

[英]Changing random number in python

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,我做了这个简单的猜数字游戏,但如果你再试一次,你会得到相同的数字。

例如,数字是 78,你猜对了,但你想再玩,所以你说你想再玩,数字仍然是 78。

那么我该怎么做才能让每次有人玩游戏时数字都会改变

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

示例:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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