Python-再次开始游戏
我正试图帮助我儿子进行简单的python项目。我们没有太多的经验,所以请尽量简单解释。 我们写了几个问题,一旦问题结束,如果用户想再次玩,那么它将带他到开始。。。 能不能请一些人帮忙?我们正努力做到: 1.如果用户键入“是”,则会将其带到开始位置,程序将再次启动。。 2.如果他输入“否”,将显示一条消息:“谢谢…”,如果可能,将退出/关闭屏幕 代码如下:Python-再次开始游戏,python,Python,我正试图帮助我儿子进行简单的python项目。我们没有太多的经验,所以请尽量简单解释。 我们写了几个问题,一旦问题结束,如果用户想再次玩,那么它将带他到开始。。。 能不能请一些人帮忙?我们正努力做到: 1.如果用户键入“是”,则会将其带到开始位置,程序将再次启动。。 2.如果他输入“否”,将显示一条消息:“谢谢…”,如果可能,将退出/关闭屏幕 代码如下: # Starting of the code import time import random def displayIntro(): pr
# 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()
谢谢。正确的缩进在Python代码中是必不可少的。函数displayIntro的语句需要缩进,否则它们不会被视为函数的一部分,并对变量使用一致的大小写(playreach)-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()
现在可以了。问题在于缩进和变量名 Python3.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()
# 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()
Python2.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()
# 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()
虽然这个答案有点笼统,但它确实回答了您关于y/n
用户输入和退出程序的特定问题
使用函数将程序划分为逻辑部分是个好主意。函数可以运行一些代码并返回None
,在这种情况下它们被称为,或者它们可以返回有用的内容
然后,您的main
函数将所有顶级函数绑定在一起。调用main()。这个if块意味着:如果这个脚本是由另一个模块导入的(可能是为了使用它的一些函数),那么不要运行main,否则“运行程序”
根据Pythons-PEP-style-guide,顶级函数由两个空行分隔,Python中的函数名是小写的,用一个下划线分隔单词
我还自由地将display\u intro
更改为intro
,因为该函数不仅打印文本,还要求输入,尽管这很简单,但您可以随意命名
有时,使用True
作为条件循环更容易编码和读取无限while循环。在这种情况下,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()
你所要做的就是
playagain = 'yes'
while playagain == 'yes':
displayIntro()
playagain = input('Do you want to play again? (yes or no)')
答案如下:
打印('您可以向上、向下、向左或向右移动')
方向=输入('输入方向:')
打印('移动'+方向)
如果没有所有空格,代码中有什么不起作用?您已经给了我们一些代码以及您希望它做什么,但是您没有告诉我们您的代码有什么问题。它不是你所期望的吗?它是否抛出异常和异常?如果是这样,什么异常-将其与堆栈跟踪一起完整发布。另外,检查代码的缩进-这是您尝试运行代码时的缩进,还是复制/粘贴代码时的错误?代码写得不好,您面临什么问题?事实上,您的代码中几乎没有错误。是否要在上重新启动脚本?太糟糕不是语言的一部分。代码中有一些错误,变量playreach
和playreach
不应混用。@Lattyware这是一个笑话,请参见链接页:goto”模块是一个愚人节玩笑,发布于2004年4月1日。是的,这很管用,但这不过是个玩笑。请不要在真实代码中使用它代码>@bouke并没有看到你们在那个里有链接,这个笑话完全超出了我的理解力。建议变量和函数使用带下划线的小写字母,类使用大写字母<代码>再次播放
将是最佳选择。谢谢,我将“再次播放”改为“再次播放”。当我运行模块时,我看不到诸如:Hello!我的名字…外壳是空的…如果我使用你的代码,我会收到这个错误消息:回溯(最近一次调用最后一次):文件“C:/Users/LIAM/Desktop/invent with python!/STS Project sun_working on the end.py”,第7行,打印('嗯,+myname+'这个程序都是关于皮肤癌的')名称错误:名称“myname”不正确defined@VishalD2.x中的函数是raw\u input()
而不是raw\u data()
。感谢您的回答。我运行您添加的代码,它在零件旁边工作,如果我单击“n”,我将收到一条错误消息。有什么建议吗?回溯(最后一次调用):sys.exit()systemexit中的文件“C:/Users/LIAM/Desktop/invent with python!/STS Project sun 5.py”,第72行。从“有东西坏了”的意义上讲,这不是“错误”,程序就是这样退出的。它抛出SystemExit
。当你得到突出显示的红色代码时,这可不好。如果单击“否”,系统是否会关闭/终止窗口?谢谢删除import sys
和sys.exit()。现在,当没有更多的代码可以运行时,程序将退出,并且没有“红色代码”。