猜数字1到100游戏编程python(猜数字1到100游戏编程python字母)
用python写猜数字小游戏
核心代码给你,具体的功能还需要自己完善。
import?time,?random
class?GuessNum:
????def?__init__(self):
????????self._num?=?''
????????self.input_num?=?[]
????????self.count?=?1??????????????????????????????????????#猜对所用次数
????????self.sec?=?0???????????????????????????????????????????#猜对所用时间
????????self._generate_num()
????def?_generate_num(self):????????????????????????#产生不重复的四个数字
????????seq_zton?=?list(range(10))
????????for?i?in?range(0,?4):
????????????a?=?str(random.choice(seq_zton))???#选出一个数字
????????????self._num?+=?a
????????????seq_zton.remove(int(a))?????????????????#注意a的类型
????????self.sec?=?time.clock()??????????????????????????#开始计时
????def?check_answer(self):
????????return?self._num
????def?check_input(self):
????????num_pos,?num_value?=?0,?0???????????????#位置对和数值对的分别的个数
????????tmp?=?input("Please?input?the?number?you?guess(No?repetition),or?'c'?to?check?the?answer:")
????????if?tmp?==?'c':
????????????print(self.check_answer())
????????????tof?=?self.check_input()
????????????return?tof
????????elif?not?tmp.isalnum?or?not?len(tmp)?==?4:
????????????print("Wrong?format!")
????????????tof?=?self.check_input()????????????????#需要优化
????????????return?tof
????????self.input_num?=?list(tmp)
????????lst_temp?=?list(self._num)
????????if?self.input_num?==?lst_temp:??????????#猜对
????????????self.prt_vic()
????????????return?True
????????for?i?in?lst_temp:
????????????if?i?in?self.input_num:
????????????????if?lst_temp.index(i)?==?self.input_num.index(i):????????#位置也相同
????????????????????num_pos?+=?1
????????????????????num_value?+=?1
????????????????else:
????????????????????num_value?+=?1
????????self.prt_state(num_pos,?num_value)
????????self.count?+=?1
????????return?False
????def?prt_state(self,?num_pos,?num_value):
????????print("You've?got?%d?numbers?with?the?right?position?and?%d?numbers?with?the?right?value?only"?%?(num_pos,?num_value))
????def?prt_vic(self):
????????t?=?time.clock()
????????self.sec?=?t?-?self.sec
????????print("Congratulations!You?have?successfully?got?the?right?number!")
????????print("%d?times?and?%.2f?sec?in?total?to?get?the?right?answer"?%?(self.count,?self.sec))
gn?=?GuessNum()
while?True:
????ss?=?gn.check_input()
????if?ss:
????????b?=?input("Continue??y/n:")
????????if?b?==?'n':
????????????break
????????else:
????????????gn?=?GuessNum()
????????????continue
python猜数游戏:在程序中预设一个随机数?
import random
num = random.randint(0, 100) #随机数
N = 0 #访问次数
while True:
N += 1
x = int(input())
if x == num:
print("猜中了,用了{}次".format(N))
break
if x num:
print("太大了")
else:
print("太小了")
和普通的python猜数字游戏不同,用户选择数字,电脑猜,如何编写程序?
不难吧。转化一下思想就好了。
#?-*-?coding:?utf-8?-*-
__author__?=?'lpe234'
def?main():
????ranges?=?range(1,?101)
????while?True:
????????num?=?raw_input(u'请输入一个?1-100?的数字:??')
????????if?not?num.isdigit():
????????????continue
????????times?=?0
????????while?True:
????????????tags?=?raw_input(u'你输入的数字是:??{}'.format(ranges[len(ranges)/2]))
????????????times?+=?1
????????????if?tags.lower()?==?'l':
????????????????ranges?=?ranges[len(ranges)/2:]
????????????????continue
????????????elif?tags.lower()?==?'h':
????????????????ranges?=?ranges[:len(ranges)/2]
????????????????continue
????????????elif?tags.lower()?==?'c':
????????????????print(u'That\'s?it.?You?got?it?after?{}?tries'.format(times))
????????????????break
if?__name__?==?'__main__':
????main()
/usr/bin/python?/Users/lpe234/PycharmProjects/untitled/x.py
请输入一个?1-100?的数字:??93
你输入的数字是:??51l
你输入的数字是:??76l
你输入的数字是:??88l
你输入的数字是:??94h
你输入的数字是:??91l
你输入的数字是:??92l
你输入的数字是:??93c
That's?it.?You?got?it?after?7?tries
请输入一个?1-100?的数字:?
编写Python程序,生成一个0~100之间的随机数,然后让用户尝试猜测这个数字。(完整在详情)
建立猜数类,累属性随机一个一到一百的数字,然后进行判断。做完后发现还是有个次数限制更有意思,所以做了个装饰器统计次数,10次没猜出来就结束,如果不想要去掉即可。
python编程 编写程序自动生成0到100间的一个随机数,然后让参与者输入昵称和数字,最后判断谁猜得最准?
#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author:Storm_duck
@file ?:20200605-01.py
@time ?:2020/6/5 15:20
"""
"""
猜数字,看谁猜的最接近
"""
import random
def get_abs(rannum, ansnum):
return abs(ansnum - rannum)
if __name__ == "__main__":
num = random.randint(1, 100)
adic = {}
lens = 0
while True:
choice = input("What's your name?,enter to quit:")
if choice == "enter":
break
if choice != "enter":
answer = int(input("What's your guess(1-100):"))
lens += 1
if choice in adic.keys():
adic[choice] = answer
else:
adic.setdefault(choice, answer)
newlist = sorted(adic.items(), key = lambda kv: get_abs(num, kv[1]), reverse = False)
if newlist[0][1] != newlist[1][1]:
if num == newlist[0][1]:
print("{} 厉害,数字就是{}:".format(newlist[0][0], newlist[0][1]))
else:
print("数字是{},猜的最接近的是:{}".format(num, newlist[0][0]))
else:
temp = []
alist = list(zip(*newlist))[1]
t = alist[0]
for i in range(alist.count(t)):
temp.append(newlist[i][0])
astr = ",".join(temp)
if num == t:
print("{}都比较厉害,数字就是{}:".format(astr, num))
else:
print("数字是{},{}的答案相同,猜的最接近。".format(num, astr))