【发布时间】:2022-10-23 12:08:15
【问题描述】:
有人告诉我,制作项目是学习用语言编写代码而不是复制和粘贴教程的最佳方式。下面的代码是我的项目,因此您可以了解我的基本技能水平以及我需要改进的地方。谢谢
import random
bullet = random.randrange(1, 6)
print("Hello welcome to Russian Roullete\nenter 'shoot' to shoot the current slot or 'cock' to rotate the revolver")
choice = input()
if choice == ("shoot"):
if bullet == 1:
print("you are dead heheheha")
else:
print("you survived")
elif choice == ("cock"):
print("cock or shoot")
choice2 = input()
if choice2 == ("shoot"):
if bullet == 2:
print("you are dead heheheha")
else:
print("you survived")
elif choice2 == ("cock"):
print("cock or shoot")
choice3 = input()
if choice3 == ("shoot"):
if bullet == 3:
print("you are dead heheheha")
else:
print("you survived")
elif choice3 == ("cock"):
print("cock or shoot")
choice4 = input()
if choice4 == ("shoot"):
if bullet == 4:
print("you are dead heheheha")
else:
print("you survived")
elif choice4 == ("cock"):
print("cock or shoot")
choice5 = input()
if choice5 == ("shoot"):
if bullet == 5:
print("you are dead heheheha")
else:
print("you survived")
elif choice5 == ("cock"):
print("last one cock or shoot")
choice6 = input()
if choice6 == ("shoot"):
if bullet == 6:
print("you are dead heheheha")
else:
print("you survived")
elif choice6 == ("cock"):
print("you coward")
【问题讨论】:
-
欢迎来到堆栈溢出。你面前有一个伟大的下一个项目。您编写代码的方式可能有效,但编写方式效率低下。每当您发现自己一遍又一遍地重复几乎相同的代码时,通常会有更好的方法来构建该代码。作为下一个项目,我建议你弄清楚如何在你的程序中添加一个循环,这样你就可以摆脱所有的重复。无论您的用户正在翘起和射击的枪中有多少个气缸,您的代码应该几乎无需修改即可工作。
标签: python console console-application