如何在基于文本的python游戏中创建性别?

时间:2018-03-13 21:14:47

标签: python python-3.x

我刚刚开始制作基于文本的冒险游戏并开始制作介绍但遇到问题......

我希望能够让玩家选择他们的性别(这是毫无意义的,但嘿,为什么不呢)。所以我开始编程但后来遇到了问题。

这就是我所拥有的。到目前为止......

#Date started: 3/13/2018
#Description: text based adventure game

import random
import time

def main():

def displayIntro():
    print('It is the end of a 100 year war between good and evil that had 
           killed more than 80% of the total human population.')
    time.sleep(4)
    print('The man who will soon be your father was a brave adventurer who 
           fought for the good and was made famous for his heroism.')
    time.sleep(4)
    print('One day that brave adventurer meet a beautiful woman who he later 
           wed and had you.')
    time.sleep(4)

gen = ""
while gen != "1" and gen != "2": # input validation
    gen = input('Your mother had a [Boy(1) or Girl (2)]: ')
return gen


playname = input('They named you: ')


displayIntro()
main()

我也收到了错误消息:

File "<ipython-input-1-940c72b6cc26>", line 10
  def displayIntro():
    ^
IndentationError: expected an indented block

1 个答案:

答案 0 :(得分:0)

您的代码中存在大量语法错误。我建议像PyCharm一样探索一个好的IDE(集成开发环境),因为如果你错误地缩进了东西,弄错了等等,这些程序会对你大喊大叫。它们是非常有价值的。

我已编辑了您的代码,以便您更好地了解如何将程序放在一起。修复的问题包括:

(1)您的多行字符串需要以某种方式连接在一起;我使用了+运算符

(2)Python对缩进严格;你的许多街区都是不正确的缩进。请参阅https://docs.python.org/2.0/ref/indentation.html

(3)如果你真的想使用main()函数,那么你可以用规范的方式来做。这通常是一个将所有其他内容包装在一起的函数,然后在if __name__ == "__main__":下运行。

import random 
import time 


def display_intro():
    print('It is the end of a 100 year war between good and evil that had \n' +
           'killed more than 80% of the total human population. \n')
    time.sleep(4)
    print('The man who will soon be your father was a brave adventurer who \n' +
           'fought for the good and was made famous for his heroism. \n')
    time.sleep(4)
    print('One day that brave adventurer meet a beautiful woman who he later \n' +
           'wed and had you. \n')
    time.sleep(4)


def get_gender(gen=None):
    while gen != "1" and gen != "2":  # input validation
        gen = input('\nYour mother had a [Boy(1) or Girl (2)]: ')
    return gen


def main():
    display_intro()
    gender_num = get_gender()

    print("This is a test. You entered {}".format(gender_num))


if __name__ == "__main__":
    main()