利用Python开发一个“智能”猜数游戏 您所在的位置:网站首页 猜数游戏在程序中预设一个0~100之间的整数 利用Python开发一个“智能”猜数游戏

利用Python开发一个“智能”猜数游戏

2023-03-19 13:31| 来源: 网络整理| 查看: 265

利用Python开发一个“智能”猜数游戏

1.Dictionaries & Pandas 2.Logic, Control Flow and Filtering 3.程序实现界面 4.知识点总结 5.未能实现的功能

1.Dictionaries & Pandas 1.1 字典基本操作 1.1.1 Motivation for dictionaries(列表的索引操作)

提示 : Use the index()method on countries to find the index of ‘germany’. Store this index as ind_ger.

代码实现

# Definition of countries and capital countries = ['spain', 'france', 'germany', 'norway'] capitals = ['madrid', 'paris', 'berlin', 'oslo'] # Get index of 'germany': ind_ger ind_ger=countries.index('germany') # Use ind_ger to print out capital of Germany print(capitals[ind_ger])

1.1.2 Access dictionary(创建字典以及访问)

提示 : 示例 europe[‘france’]

Check out which keys are in europe by calling the –keys()method on europe. Print out the result. 打印字典的所有键Print out the value that belongs to the key 'norway'. 打印指定键‘norway’的值

代码实现

# Definition of dictionary europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' } # Print out the keys in europe print(europe.keys()) # Print out value that belongs to key 'norway' print(europe['norway'])

结果

dict_keys(['norway', 'spain', 'france', 'germany']) oslo

1.1.3 Dictionary Manipulation1(字典操作01) 1.1.4 目标值随机生成 1.1.5 程序可循环执行

1.2 拓展要求 1.2.1 有图形化界面 1.2.2对非数字字符输入有处理(例如:提示输入非法,请重新输入)

2. 代码解释

from tkinter import * # 从tkinker 导入所有的函数 import tkinter.simpledialog as dl import tkinter.messagebox as mb import random #为产生随机数导入random模块 root = Tk() #初始化Tk() w = Label(root,text = "Guess Number") w.pack() mb.showinfo("Welcome","Welcome To Guess Number Game") #设置消息框的标题及文字 number =random.uniform(1, 100) #产生1到100的实数 number=int(number) #将产生的随机数转化为整数 min=1 #设置提示区间的最小值 max=100 #设置提示区间的最大值 t=0 #设置计数器 while True: guess = dl.askfloat("Number","What's your guess?") guess=int(guess) t=t+1 if guess == number: output = "Bingo! you are right!\nAnd you have tried "+str(t)+' times' mb.showinfo("Hint",output) break elif min


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有