GUI-猜字游戏
# 设计思想先定义输入,输入,然后设计逻辑 # GUI from tkinter # 逻辑层 # 设计GUI(用户界面) from tkinter import * import tkinter.simpledialog as dl import tkinter.messagebox as md root = Tk() w = Label(root, text='Guess Number Game') w.pack() md.showinfo('welcome','welcome to Guess Number Game') # guess = dl.askinteger('number', 'Enter a number') number = 59 while True: guess = dl.askinteger('Number', "what's your guess?") if guess == number: output = 'Bingo! you guess it right,but you do not win any prizes' md.showinfo('Hint:' ,output) break elif guess < number: output = 'No,the number is a higher than that' md.showinfo('Hint:', output) else: output = 'No, the number is a lower than that' md.showinfo('Hint:', output) print('Done') output = 'this is output message' md.showinfo('output',output)
这里提供另一种方法:
import random # random.randint(-1,10) #-1,0,1,2,3,4,5,6,7,8,9,10 computer = random.randint(0,2) #站在用户的角度上 # 用户赢 # 0-->2 1-->0 2-->1 # 用户和电脑打平0 # 0-->0 1-->1 2-->2 # 用户输 # 除了上面所有的情况,都是输 if (person == 0 and computer == 2)or(person == 1 and computer == 0)or(person == 2 and computer == 1): print('你赢了') elif person == computer: print('平手') else: print('你输了')