Python - 再次开始游戏

时间:2013-07-27 11:21:02

标签: python

我正在尝试用简单的python项目帮助我的儿子。我们没有太多经验,所以请尽量解释一下。 我们写了几个问题,一旦问题结束,那么如果用户想要再次播放,那么它将把他带到一开始...... 有人可以帮忙吗?我们试图得到: 1.如果用户输入“是”,则会将他带到开头,程序将重新开始。 2.如果他输入“否”,它会给出一条消息:“谢谢......”如果可能的话会退出/关闭屏幕......

以下是代码:

# Starting of the code
import time
import random
def displayIntro():
print('Hello! My name is John. What is your name?')
myname = input()
print ('Well, ' +myname + ' This program is all about skin cancer.')

# some question below

#End of the code
playagain = 'yes'
while playagain == 'yes': 
    displayIntro()
    print('Do you want to play again? (yes or no)')
    playAgain = input()

谢谢。

5 个答案:

答案 0 :(得分:1)

这将立即生效。问题在于缩进和变量名称。

for python 3.x

# Starting of the code

import time
import random
def displayIntro():
    print('Hello! My name is John. What is your name?')
    myname = input()
    print ('Well, ' + myname + ' This program is all about skin cancer.')

    # some question below

#End of the code

playagain = 'yes'
while playagain == 'yes': 
    displayIntro()
    print('Do you want to play again? (yes or no)')
    playagain = input()

for python 2.x

# Starting of the code

import time
import random
def displayIntro():
    print('Hello! My name is John. What is your name?')
    myname = raw_input()
    print ('Well, ' + myname + ' This program is all about skin cancer.')

    # some question below

#End of the code

playagain = 'yes'
while playagain == 'yes': 
    displayIntro()
    print('Do you want to play again? (yes or no)')
    playagain = raw_input()

答案 1 :(得分:0)

在Python代码中,正确的缩进是必不可少的。函数displayIntro的语句需要缩进,否则它们不会被视为函数的一部分,并且对变量使用一致的大小写(playAgain) - Python区分大小写:

# Starting of the code
import time
import random

def displayIntro():
    print('Hello! My name is John. What is your name?')
    myname = input()
    print('Well, ' +myname + ' This program is all about skin cancer.')

# some question below

#End of the code
playAgain = 'yes'
while playAgain == 'yes': 
    displayIntro()
    print('Do you want to play again? (yes or no)')
    playAgain = input()

答案 2 :(得分:0)

虽然这个答案更为通用,但它确实回答了您在y/n用户输入和退出程序时的具体问题。

使用函数将程序分成逻辑部分是个好主意。函数可以运行一些代码并返回None,在这种情况下它们被称为procedures,或者它们可能会返回一些有用的代码。

然后,您的main功能将所有顶级功能绑定在一起。基本上调用main()'运行程序',在if __name__ == '__main__'块内调用。这个if块意味着:如果这个脚本是由另一个模块导入的(也许是为了使用它的一些功能),那么就不要运行main,否则运行程序'。

根据Pythons PEP样式指南,顶级函数由两个空行分隔,Python中的函数名称为小写,单个下划线分隔单词。

我也冒昧地将display_intro更改为intro,因为该功能并不仅仅打印文本,它还要求输入,虽然这大部分是微不足道的,但您可以将其命名为你喜欢。

有时使用True作为循环条件,更容易编码和读取无限循环。在这种情况下,returnbreak会将执行移出循环。

import time
import random
import sys


def intro():
    print('Hello! My name is John. What is your name?')
    myname = input()
    print ('Well, ' + myname + ' This program is all about skin cancer.')


# some question below


def play_again():
    """Returns True or False"""
    while True:
        # As a convention the capital Y indicates that 
        # hitting enter without any input means yes; yes is default.
        answer = input("Do you want to play again? (Y/n): ")
        if not answer or answer.lower() in ('y', 'yes'):
            return True
        elif answer.lower() in ('n', 'no'):
            return False
        else:
            print("Not a valid answer!")


def main():
    while True:
        intro()
        if not play_again():
            return


if __name__ == '__main__':
    main()
    sys.exit()

答案 3 :(得分:0)

你所要做的就是......

playagain = 'yes'
while playagain == 'yes': 
    displayIntro()
    playagain = input('Do you want to play again? (yes or no)')

答案 4 :(得分:0)

这是答案:

print('您可以上下左右移动。')

direction = input('输入方向:')

打印(“您向+方向移动”

尽管没有所有空格