我是初学程序员。我想创建一个用户输入影响游戏过程的游戏。我有点卡在一开始。
def displayIntro():
print("You wake up in your bed and realize today is the day your are going to your friends house.")
print("You realize you can go back to sleep still and make it ontime.")
def wakeUp():
sleepLimit = 0
choice = input("Do you 1: Go back to sleep or 2: Get up ")
for i in range(3):
if choice == '1':
sleepLimit += 1
print("sleep")
print(sleepLimit)
if sleepLimit == 3:
print("Now you are gonna be late, get up!")
print("After your shower you take the direct route to your friends house.")
elif choice == '2':
print("Woke")
whichWay()
else:
print("Invalid")
def whichWay():
print("After your shower, you decide to plan your route.")
print("Do you take 1: The scenic route or 2: The quick route")
choice = input()
if choice == 1:
print("scenic route")
if choice == 2:
print("quick route")
displayIntro()
wakeUp()
我有一些错误,而且我已经尝试过自己解决这些问题,但我还在挣扎。
1)我只希望玩家能够重新入睡3次,而在第三次我希望出现一条消息并运行另一个功能(尚未制作)。
2)如果玩家决定醒来我想要whoWay()运行而且它确实运行但不是退出那个for循环它会回到那个循环并询问玩家是否想再次醒来我不知道如何解决这个问题。
3)有没有更好的方法来制作这样的游戏?
感谢您的时间,希望您的回答。
答案 0 :(得分:0)
以下代码应该有效
1.我把'选择=输入(你是1:回去睡觉还是2:起来)''线移到for循环中。
我在elif块的末尾添加了一个break语句。
def wakeUp():
sleepLimit = 0
for i in range(3):
choice = input("Do you 1: Go back to sleep or 2: Get up ")
if choice == '1':
sleepLimit += 1
print("sleep")
print(sleepLimit)
if sleepLimit == 3:
print("Now you are gonna be late, get up!")
print("After your shower you take the direct route to your friends house.")
elif choice == '2':
print("Woke")
whichWay()
break
else:
print("Invalid")