Home  >  Article  >  Backend Development  >  Use Python to implement a simple guessing number game sample code

Use Python to implement a simple guessing number game sample code

高洛峰
高洛峰Original
2017-03-20 11:22:104528browse

Python Implement a simple number guessing game, as follows:

Randomly generate a number between 1-10 for the user to guess. When the guess is wrong, the guesser will be prompted. Whether the number is larger or smaller until the user guesses correctly.

import random
secret = random.randint(1,10)
#print(secret)
print('------猜数字游戏!-----')
guess = 0
while guess != secret:
    temp = input('猜数字游戏开始,请输入数字:')
    guess = int(temp)
    if guess > secret:
        print('您输入的数字大了!')
    else:
        print('您输入的数字小了!')
if guess == secret:
    print('恭喜,您猜对了!')
    print('游戏结束,再见!^_^')

The above is the detailed content of Use Python to implement a simple guessing number game sample code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn