-1

我正在尝试通过简单的 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()

谢谢你。

4

5 回答 5

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()
于 2013-07-27T11:28:40.177 回答
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()
于 2013-07-27T11:26:52.340 回答
0

虽然这个答案有点笼统,但它确实回答了您关于y/n用户输入和退出程序的具体问题。

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

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

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

我还冒昧地更改display_intro为,intro因为该函数不仅打印文本,它还要求输入,虽然这大多是微不足道的,你可以随意命名它。

有时,使用条件循环更容易编码和读取无限 while 循环True。在这种情况下return,或者break将执行移出循环。

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()
于 2013-07-27T12:21:37.927 回答
0

这是答案:

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

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

print('你移动' + 方向)

虽然没有所有的空间

于 2020-04-29T02:50:33.813 回答
0

你所要做的就是...

playagain = 'yes'
while playagain == 'yes': 
    displayIntro()
    playagain = input('Do you want to play again? (yes or no)')
于 2016-02-16T17:54:34.240 回答