基本Python数据验证



所以我正在做GSCE,我们的任务是创建一个简单的、基于文本的游戏。我试着添加一些数据验证,向老师展示我能做到,但结果。。。我做不到……对于任何一个精通Python的人来说,这似乎真的很容易,但出于某种原因,当我运行程序时,它有一个"无效语法",所以我解决了这个问题(只需添加一个":"(,但它说缩进是错误的?数据验证接近开始,您可以选择"向左"或"向右";如果你输入了"左"或"右"以外的任何内容,我希望它能再次给你这个问题。。。请有人帮忙!(也请忽略垃圾故事(。

import time
import sys
print("Mars: 2040. The human race has been wiped out of existence by the alien race. Will you suffer the same fate?")
time.sleep(3)
name=input("What is your name?")
def start()
print("Okay",name,", which direction would you like to go? Left or right?")
direction=input()
if direction == "left":
print("You turn the corner to discover a large, wolf-life monster. It erupts in deafening roar and proceeds towards you.")
print("You enter the room and see a bow (and arrow) on the ground next to a sword. Which do you take? *Bow* or *sword*?")
weapon=input()
if weapon == "bow":
print("You fire the arrow into one of the eyes of the large monster. It stumbles, but stays on it's feet")
print("Nice hit! What now? *Shoot again* or *reach for your pocket*?")
weapon2=input()
if weapon2 == "shoot again":
print("You pull back the string and release. It soars over the head of the monster. The monster raises it's leg and stamps on your puny head.")
time.sleep(3)
print("GAME OVER")
exit()
if weapon2 == "reach for your pocket":
print("You pull out a sharp knife and throw it towards the monster. it strikes the leg of the monster and it crumbles to the ground. You run over, grabbing th eknife as you do and stab the monster in it's red, glowing eye. You win!")
time.sleep(4)
print("Congratulations! You win!!1! :D")
print("Game created by Tom Jolley.")
elif weapon == "sword":
print("You take a large, over-exaggerated swing... the sword falls to the ground with a loud bang as you dropped it 1 foot away from you because it was ''too heavy''... the monster stares at you you for a moment and it stamps on your head.")
time.sleep(5)
print("GAME OVER")
exit()
elif direction == "right":
print("You turn the corner to discover a ginormous arachnid drooling and staring at you with it's 8 glowing red eyes. It proceeds towards you.")
print("...well no, that's not a direction... just stand there..")
print("You enter the room and see a bow (and arrow) on the ground next to a sword. Which do you take? *Bow* or *sword*?")
weapon=input()
if weapon == "bow":
print("You fire the arrow into one of the eyes of the large monster. It stumbles, but stays on it's feet")
print("Nice hit! What now? *Shoot again* or *reach for your pocket*?")
weapon2=input()
if weapon2 == "shoot again":
print("You pull back the string and release. It soars over the head of the monster. The monster raises it's leg and stamps on your puny head.")
time.sleep(3)
print("GAME OVER")
exit()
if weapon2 == "reach for your pocket":
print("You pull out a sharp knife and throw it towards the monster. it strikes the leg of the monster and it crumbles to the ground. You run over, grabbing th eknife as you do and stab the monster in it's red, glowing eye. You win!")
time.sleep(4)
print("Congratulations! You win!!1! :D")
print("Game created by Tom Jolley.")
elif weapon == "sword":
print("You take a large, over-exaggerated swing... the sword falls to the ground with a loud bang as you dropped it 1 foot away from you because it was ''too heavy''... the monster stares at you you for a moment and it stamps on your head.")
time.sleep(5)
print("GAME OVER")
exit()

else:
print()
start()
start()

这里主要是间距问题,有很多8个空格而不是4个,有些地方是5个

import time
import sys
print("Mars: 2040. The human race has been wiped out of existence by the alien race. Will you suffer the same fate?")
time.sleep(3)
name=input("What is your name?")
def start()
print("Okay",name,", which direction would you like to go? Left or right?")
direction=input()
if direction == "left":
print("You turn the corner to discover a large, wolf-life monster. It erupts in deafening roar and proceeds towards you.")
print("You enter the room and see a bow (and arrow) on the ground next to a sword. Which do you take? *Bow* or *sword*?")
weapon=input()
if weapon == "bow":
print("You fire the arrow into one of the eyes of the large monster. It stumbles, but stays on it's feet")
print("Nice hit! What now? *Shoot again* or *reach for your pocket*?")
weapon2=input()
if weapon2 == "shoot again":
print("You pull back the string and release. It soars over the head of the monster. The monster raises it's leg and stamps on your puny head.")
time.sleep(3)
print("GAME OVER")
exit()
if weapon2 == "reach for your pocket":
print("You pull out a sharp knife and throw it towards the monster. it strikes the leg of the monster and it crumbles to the ground. You run over, grabbing th eknife as you do and stab the monster in it's red, glowing eye. You win!")
time.sleep(4)
print("Congratulations! You win!!1! :D")
print("Game created by Tom Jolley.")
elif weapon == "sword":
print("You take a large, over-exaggerated swing... the sword falls to the ground with a loud bang as you dropped it 1 foot away from you because it was ''too heavy''... the monster stares at you you for a moment and it stamps on your head.")
time.sleep(5)
print("GAME OVER")
exit()
elif direction == "right":
print("You turn the corner to discover a ginormous arachnid drooling and staring at you with it's 8 glowing red eyes. It proceeds towards you.")
print("...well no, that's not a direction... just stand there..")
print("You enter the room and see a bow (and arrow) on the ground next to a sword. Which do you take? *Bow* or *sword*?")
weapon=input()
if weapon == "bow":
print("You fire the arrow into one of the eyes of the large monster. It stumbles, but stays on it's feet")
print("Nice hit! What now? *Shoot again* or *reach for your pocket*?")
weapon2=input()
if weapon2 == "shoot again":
print("You pull back the string and release. It soars over the head of the monster. The monster raises it's leg and stamps on your puny head.")
time.sleep(3)
print("GAME OVER")
exit()
if weapon2 == "reach for your pocket":
print("You pull out a sharp knife and throw it towards the monster. it strikes the leg of the monster and it crumbles to the ground. You run over, grabbing th eknife as you do and stab the monster in it's red, glowing eye. You win!")
time.sleep(4)
print("Congratulations! You win!!1! :D")
print("Game created by Tom Jolley.")
elif weapon == "sword":
print("You take a large, over-exaggerated swing... the sword falls to the ground with a loud bang as you dropped it 1 foot away from you because it was ''too heavy''... the monster stares at you you for a moment and it stamps on your head.")
time.sleep(5)
print("GAME OVER")
exit()

else:
print()
start()
start()

在python中,缩进(一行开头的空白(有很大的不同。这两个代码块看起来非常相似,但做的事情不同

for x in xs:
if x == "foo":
good()
else:
bad()

for x in xs:
if x == "foo":
good()
else:
bad()

要回答您的问题,请查看第14行的代码。为了清楚起见,我已经将上的空格("(替换为">">

if direction == "left":
print("You turn the corner to discover a large, wolf-life monster. It erupts in deafening roar and proceeds towards you.")
>>>>>>>>>>>>print("You enter the room and see a bow (and arrow) on the ground next to a sword. Which do you take? *Bow* or *sword*?")
weapon=input()
if weapon == "bow":
print...

注意到它与上面的线有不同数量的空间吗?除非你在一个街区内,否则这种情况永远不应该发生。如果前一行以冒号":"结尾,您将确信自己在块内。

要修复代码,请确保同一块中的所有内容都有相同的缩进量。我会把你的代码改成的样子

if direction == "left":
print("You turn the corner to discover a large, wolf-life monster. It erupts in deafening roar and proceeds towards you.")
print("You enter the room and see a bow (and arrow) on the ground next to a sword. Which do you take? *Bow* or *sword*?")
weapon=input()
if weapon == "bow":
print...

为了参考,这里有一些正确缩进的代码片段的示例。请注意,在上一个示例中,只使用了一个空格,而不是四个空格。只要块内的每一行的空格数都相同,那么空格的确切数目就无关紧要了。

for line in textfile:
process(line)
i = 0
while i < 5:
i += 1
print(i)
with open("testresults.csv", 'r') as f:
results = f.readlines()
if good(results):
print("Yay!")
else:
print("Aww")
i = 0
while i < 5:
i += 1
print(i)

相关内容

  • 没有找到相关文章

最新更新