python猜数字游戏console版本

加入python学习小组后的第一次作业,python GUI写猜数字游戏。由于加班比较多,第一步先实现console版本,下一步再实现GUI版本。

虽然猜数字游戏是个小游戏,但是涉及到的基础知识点还是很多的,如下。对初学者来说可以起到一个很好的巩固作用

·        import statements

·        Modules

·        while statements

·        Conditions

·        Blocks

·        Booleans

·        Comparison operators

·        if statements

·        The break keyword

·        The str()and int()and float() functions

·        The random.randint() function

·        Try except 

·        Input(), title(), string format

# coding=utf-8
# Name: Jacky.lu
# date: 2019-01-22
# Description: A number guessing game!
import random


name = input('Hello, what is your name?\n')
number = random.randrange(1, 100)
print("Hi {}, I'm thinking of a number between 1 and 100.\n".format(name.title()))
guessesTaken = 0

while True:
guess = input('Please enter a guess: ')
guessesTaken += 1
try:
guess = int(guess)
print('Yes input string is an Integer.\n')
print('Input number value is: {}'.format(guess))

if guess < number:
print("That was too low!")
elif guess > number:
print("That was too high!")
else:
break

except ValueError:
print("That's not a digit! Please input a number again!\n")

if guess == number:
print("Congratulate {}, you guessed the correct number!".format(name))
else:
print("You lose, too bad. Beter luck next time. The right number was {}.".format(number))

猜你喜欢

转载自www.cnblogs.com/lufay/p/10319445.html
今日推荐