python - 如何修复 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')
它说'无效语法,并且正在定义的'问题'之后出现一条红线。这只是为了给用户第二次机会来做出他们做出的决定
解决方案
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(): 其次,您需要将满意答案与是进行比较。
推荐阅读
- c - BST 表实现段错误(C)
- javascript - 在 React Native 中关闭广告时如何执行功能?
- node.js - 在呈现页面之前不保存闪存消息(节点)
- python - Python在字典上实现循环
- mysql - 如何从docker连接到主机上的mysql?
- ruby-on-rails - Devise::FailureApp 的未定义方法“admin”
- c++17 - 使用 std::variant 的 StateMachine,获得正确的自定义模板推导
- java - JPA @Query java.lang.IllegalArgumentException:
- sql - 使用约束引用主键
- python - 即使我选择单个字符串元素,“列表”对象也没有属性“条带”错误?