为什么我的随机数字游戏无法正常工作?

时间:2018-09-01 07:58:31

标签: python python-3.x

伙计们!我是新来的。很高兴见到你!

我有以下问题。我的随机数生成器总是做完全相同的数字。我猜为什么会这样,但我不确定。能否请您解释一下我做错了什么,只写些关于我的代码的话?谢谢!

import random


def begin():
    r_num = random.randint(1, 10)
    p_num = int(input('Enter your number: '))
    ch = (r_num, p_num)
    if ch[0] == ch[1]:
        print(ch[0])
        print("You've won! Excellent!")
        play_again = input("Do you want to play again? y/n")
        if play_again == 'y' or play_again == 'Y':
            begin()
        elif play_again == 'n' or play_again == 'N':
            print('Goodbye!')
    elif ch[1] < ch[0]:
        print(ch[0])
        print("Your number is lower than it must be!")
        begin()
    elif ch[1] > ch[0]:
        print(ch[0])
        print("Your number is higher than it must be!")
        begin()


def start():
    gen = input('Print generate to begin playing! \n')
    if gen == 'generate':
        print('Success!')
        begin()
    else:
        print('Fail!')
        start()


print('Welcome to my first Python game! Guess random generated number from 1 to 10!')
start() 

1 个答案:

答案 0 :(得分:1)

该代码可以正常工作,因此您真的必须拥有令人难以置信的好/坏运气(取决于您对它的看法)。尝试在其他IDE中运行代码-这是我唯一想到的可能导致此问题的方法。尝试再次运行脚本?