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

Use Python to implement a simple guessing number game sample code

高洛峰
Release: 2017-03-20 11:22:10
Original
4578 people have browsed it

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('游戏结束,再见!^_^')
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template