简体   繁体   English

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

[英]Why my random number game doesn't work right?

guys! 伙计们! I'm new here. 我是新来的。 Nice to meet you! 很高兴见到你!

I have the following problem. 我有以下问题。 My random number generator always do exactly the same number. 我的随机数生成器总是做完全相同的数字。 I have a guess why it's happening, but I'm not sure. 我猜为什么会这样,但我不确定。 Can you, please, explain what I'm doing wrong and just write some words about my code at all? 能否请您解释一下我做错了什么,只写些关于我的代码的话? Thanks! 谢谢!

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

The code works perfectly fine and so you genuinely must just have had incredibly good/bad luck (depending on which way you think about it). 该代码可以正常工作,因此您真的必须拥有令人难以置信的好/坏运气(取决于您对它的看法)。 Try running the code in a different IDE - it's the only thing I can think of that may be causing the issue. 尝试在其他IDE中运行代码-这是我唯一想到的可能导致此问题的方法。 Try running the script again? 尝试再次运行脚本?

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

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