简单条件下的语法错误(如果partyCHoice = R :)
问题描述:
新程序员,在python 2.7中工作。简单条件下的语法错误(如果partyCHoice = R :)
使用此代码,我在'if partychoice = R'行中得到语法错误,指出'='是无效语法。它如何赢得;让我分配变量。
此外,我敢肯定还有很多其他错误,但我必须从某处开始。
print "Welcome to 'Pass the Bill', where you will take on the role of a professional lobbyist trying to guide a bill through Congress and get it signed into law by the President!"
start = raw_input(str ('Press Y to continue:'))
print 'Great, lets get started!'
partychoice = raw_input (str("Would you like to be a Republican or Democrat? Type 'R' for Republican or 'D' for Democrat."))
if partychoice = R:
print 'Ah, the Grand old Party of Lincoln and Reagan. Great Choice!'
replegchoice = raw_input (str("What type of bill what you like congress to introduce? Restrictions on abortions, lower income taxes, easier access to automatic weapons, private health plans, or stricter immigration laws? (A = abortion restrictions, L = lower taxes, AW = automatic weapons, H = private health plans, S = stricter immigration laws'))
if replegchoice = A or a
print 'A controversial choice, despite support of most Republicans, you are sure to face opposition from Democrats in Congress!'
if replegchoice = L or l
print 'A popular idea, Republicans in Congress are sure to support this idea, as will many American voters!'
if replegchoice = AW, aw, Aw, or AW
print 'Rural, midwest, and small town voters will love this, as will most Republicans in Congress. Democrats and voters in urban cities will surely be less supportive.'
if replegchoice = H or h
print 'Eimination of Medicare, Medicaid, and Obamacare! Republicans generally like the idea of making each person responsible for paying their own health care costs'
if replegchoice = S or s
print 'a popular idea supported by president Trump, this is sure face strong opposition from democrats and many voters.'
谢谢大家。
答
您需要的语句来代替 “=”, “==”:
if partychoice = R:"
“=” 是赋值运算符
“==” 是一个平等的运营商
eg
#assign something to a variable
x = 5
print x
>>5
#compare for equality
y = 6
if y == 6:
print y
else:
print "y is not 6"
>>6
请务必在将来的帖子中使用信息标题,这些内容与您提问的问题有关。
答
行更改为
if partychoice == 'R':
首先,你需要使用两个 '=' 字符。一个'='设置一个变量,两个比较是否相等。
其次要变量partychoice
比较字符串“R”,所以你需要引号。没有引号,它认为你正在比较对另一个对象的引用。
答
对于条件您需要使用比较运算符==
而不是赋值运算符=
。
检查了这一点:https://www.tutorialspoint.com/python/python_basic_operators.htm
希望帮助!
答
有几个问题与代码:
- 您正在使用赋值运算符,
=
,而不是比较操作,==
。 - 你需要记住使用
'
含和比较字符串时,您正在将其与可变R
代替字母'R'
- 我删除了
raw_input
函数内superfulousstr()
转换比较。这是不需要的,因为""
定义了一个字符串。 -
您应该简单地在结果上调用
.lower()
函数,这样您只需检查字符串的小写版本即可。它会为你节省很多时间。print "Welcome to 'Pass the Bill', where you will take on the role of a professional lobbyist trying to guide a bill through Congress and get it signed into law by the President!" start = raw_input('Press Y to continue:') print 'Great, lets get started!' partychoice = raw_input("Would you like to be a Republican or Democrat? Type 'R' for Republican or 'D' for Democrat.").lower() if partychoice == 'r': print 'Ah, the Grand old Party of Lincoln and Reagan. Great Choice!' replegchoice = raw_input ("What type of bill what you like congress to introduce? Restrictions on abortions, lower income taxes, easier access to automatic weapons, private health plans, or stricter immigration laws? (A = abortion restrictions, L = lower taxes, AW = automatic weapons, H = private health plans, S = stricter immigration laws") if replegchoice == 'a': print 'A controversial choice, despite support of most Republicans, you are sure to face opposition from Democrats in Congress!' if replegchoice == 'l': print 'A popular idea, Republicans in Congress are sure to support this idea, as will many American voters!' if replegchoice == 'aw': print 'Rural, midwest, and small town voters will love this, as will most Republicans in Congress. Democrats and voters in urban cities will surely be less supportive.' if replegchoice == 'h': print 'Eimination of Medicare, Medicaid, and Obamacare! Republicans generally like the idea of making each person responsible for paying their own health care costs' if replegchoice == 's': print 'a popular idea supported by president Trump, this is sure face strong opposition from democrats and many voters.'
它应该是''==而不是'=',即'如果partychoice ==“R'',同去的嵌套'if's –
修改标题。这与你的问题无关 – Noorul
另外:如果你是一个新的程序员,你应该从Python 3开始。 – DSM