基本的Python数据验证

时间:2018-12-06 17:34:10

标签: python validation

所以我正在做我的GSCE,我们的任务是创建一个简单的基于文本的游戏。我试图添加一些数据验证来向我的老师展示我可以做到,但是事实证明……我做不到……对于任何精通Python的人来说,由于某种原因,这似乎都很容易当我运行程序时,它具有“无效语法”,因此我解决了该问题(只需添加“:”),但随后它说缩进是错误的吗?数据验证即将开始,您可以选择“左”或“右”。如果您键入“ left”或“ right”以外的其他内容,我希望它再次给您一个问题。请有人帮助! (也请忽略垃圾的故事。)

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()

2 个答案:

答案 0 :(得分:0)

这里主要是间距问题,有很多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()

答案 1 :(得分:0)

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

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

vs

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)