新手入门python,想写个自己的小游戏,那就写个石头剪刀布小游戏吧,开始,上代码
#石头剪刀布程序 import random; chuquan=["石头","剪刀","布"]; computer=random.choice(chuquan); print("机器出拳是:",computer); people = '' people=input('请出拳:(石头、剪刀、布)'); while people not in chuquan: print('输入有误,请重新出拳') # 当用户输入错误,提示错误,重新输入 people= input() if people==computer: print("平局") elif (people=="石头" and computer=="剪刀") or (people=="剪刀" and computer=="布") or (people=="布" and computer=="石头"): print("您赢了"); else: print("您输了");
另外带充值检测判断:
#石头剪刀布带充值游戏 import random print("======欢迎来到游戏中心======") money=int(input("请充值金币:")) print("===充值成功,开始游戏===") while money> 0: print("===开始游戏===") chooce=["石头","剪刀","布"]; computer_chooce=random.choice(chooce) people_choice=input("您出:石头,剪刀,布?"); #判断用户输入是否错误 while people_choice not in chooce: print('输入有误,请重新出拳') user_choice = input() if people_choice==computer_chooce: print("平局,电脑出",computer_chooce); elif (people_choice=="石头" and computer_chooce=="剪刀") or (people_choice=="剪刀" and computer_chooce=="布") or (people_choice=="布" and computer_chooce=="石头"): print("您赢了!电脑出:",computer_chooce); money += 1; print("恭喜加一元!",money) else: money -= 1; print("您输了!电脑出",computer_chooce) print("恭喜减一元!", money) cishu=input("您是否要继续?继续按回车键,退出按n"); if cishu=='n': break;