
正文
Python数字游戏函数,python猜数字游戏编程
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
用python 3.6 弄个游戏, 两个玩家互相从1到9选数字,直到有个人选的数字加起来等于15
这里只是给出一个简单的例子,可能写的比较繁琐,但是基本功能上实现了。这里没有对玩家输入的数据进行验证,比如输入字符或者1到9之外的数据。另外, 如果可以,可以尝试把各个模块独立出来,比如将验证用户输入独立成一个函数,如果验证失败,则让其再次输入,验证成功,则继续下一步,等等。
#sum15.py
total = 0
while True:
print('当前总数为:{0}'.format(total))
v1 = input('玩家A请选择,1~9任意数值:\n')
total += int(v1)
if total == 15:
print('玩家A获得胜利,游戏结束')
break
elif total 15:
print('总数大于15,游戏结束')
break
print('当前总数为:{0}'.format(total))
v2 = input('玩家B请选择,1~9任意数值\n')
total += int(v2)
if total == 15:
print('玩家B获得胜利,游戏结束')
break
elif total 15:
print('总数大于15,游戏结束')
break
相关问答
Q1: python简单小游戏代码 怎么用Python制作简单小游戏
1、Python猜拳小游戏代码:
2、import random #导入随机模块
3、
4、num = 1
5、yin_num = 0
6、shu_num = 0
7、while num 2:
12、 print('不能出大于2的值')
13、 else:
14、 data = ['石头', '剪刀', '布']
15、 com = random.randint(0, 2)
16、 print(您出的是{},电脑出的是{}.format(data[user], data[com]))
17、 if user == com:
18、 print('平局')
19、 continue
20、 elif (user == 0 and com == 1) or (user == 1 and com == 2) or (user == 2 and com == 0):
21、 print('你赢了')
22、 yin_num += 1
23、 else:
24、 print('你输了')
25、 shu_num += 1
26、 num += 1
27、Python数字炸弹小游戏代码:
28、import random
29、import time
30、
31、bomb = random.randint(1, 99)
32、print(bomb)
33、start = 0
34、end = 99
35、while 1 == 1:
36、
37、 people = int(input('请输入{}到{}之间的数:'.format(start, end)))
38、 if people bomb:
39、 print('大了')
40、 end = people
41、 elif people bomb:
42、 print('小了')
43、 start = people
44、 else:
45、 print('BOOM!!!')
46、 break
47、 print('等待电脑了输入{}到{}之间的数:'.format(start, end))
48、 time.sleep(1)
49、 com = random.randint(start + 1, end - 1)
50、 print('电脑输入:{}'.format(com))
51、 if com bomb:
52、 print('大了')
53、 end = com
54、 elif com bomb:
55、 print('小了')
56、 start = com
57、 else:
58、 print('BOOM!!!')
59、 break
Q2: 是一个关于Python的问题,设计一个猜数游戏
# coding=utf8
import random
MAXCOUNT = 10
def run(num1, num2, count):
if num1 == num2:
print "恭喜你!猜中了!共猜了" + str(count) + "次!"
return True
elif num1 num2:
print "猜错了!数字更小些!还有" + str(MAXCOUNT - count) + "次机会!"
else:
print "猜错了!数字更大些!还有" + str(MAXCOUNT - count) + "次机会!"
return False
def main():
print "********* 游戏开始 *********"
print "** 1. 初级(数在0 ~ 9之间)"
print "** 2. 中级(数在0 ~ 99之间)"
print "** 3. 高级(数在0 ~ 999之间)"
tp = raw_input('** 请选择游戏等级:').strip()
if tp == "1": max_num = 9
elif tp == "2": max_num = 99
elif tp == "3": max_num = 999
else:
print "输入错误!"
return
num = random.randint(0, max_num)
print "随机数字已经产生,下面进入参数环节\n"
for i in range(1, MAXCOUNT + 1):
n = raw_input('** 请猜数:').strip()
if not n.isdigit():
print "输入错误!"
continue
flag = run(int(n), num, i)
if flag:
break
else:
print "超过" + str(MAXCOUNT) + "次没有猜中,很遗憾!"
print "\n********* 游戏结束 *********"
if __name__ == "__main__":
main()
很简单的逻辑 没写注释了 理一下应该就能清楚
Q3: 如何用python程序编写一个循环五次的小游戏,每次随机产生两个数字让用户计算这两个数的?
如果这是VB的程序,那么可以用如下的得到两个1~10的整数:
x=Int(Rnd*10)+1。
y=Int(Rnd*10)+1。
再加上循环for i=1 to 5可以。
#include stdio.h。
#include stdlib.h bai/* 随机库函数 */。
#include time.h /* 时间库函数 */。
int count=0, ok=0; /* count表示回答次数, ok表示回答正确次数 */。
/* 加法函数 */。
int add(int x,int y)。
优点
简单:Python是一种代表简单主义思想的语言。阅读一个良好的Python程序就感觉像是在读英语一样。它使你能够专注于解决问题而不是去搞明白语言本身。
易学:Python极其容易上手,因为Python有极其简单的说明文档。
易读、易维护:风格清晰划一、强制缩进。
Q4: 用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






