首页 > 解决方案 > 如何修复 Python 中的“无效语法”错误?

问题描述

我正在为一个简单的骰子游戏编写代码(因为我对 python 比较陌生)并且代码的一小部分发生了“无效语法”错误,我不知道为什么。这个问题曾经出现在它说“问题=输入”的那一行,所以我添加了定义函数。我不知道这是否是正确的方法

我试图改变位置,添加一个冒号并完全切换代码

def question


satisfaction = (player1, ' are you happy with your result?')
satisfactionanswer = input(satisfaction)
if satisfaction == 'yes':
    question = input('Are you sure?')
if question == 'yes':
    print ('Very well')

它说'无效语法,并且正在定义的'问题'之后出现一条红线。这只是为了给用户第二次机会来做出他们做出的决定

标签: python

解决方案


player1 = 'Caleb'
def question():
    satisfaction = (player1, ' are you happy with your result?')
    satisfactionanswer = input(satisfaction)
    if satisfactionanswer == 'yes':
        question = input('Are you sure?')
    if question == 'yes':
        print ('Very well')

一些评论,一个函数定义为 def question(): 其次,您需要将满意答案与是进行比较。


推荐阅读