简单if条件的语法错误(if partychoice=R:)

2024-05-23 21:04:56 发布

您现在位置:Python中文网/ 问答频道 /正文

新程序员,使用python2.7。你知道吗

在这段代码中,我在'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.'

谢谢大家。你知道吗


Tags: orandofthetoinyouif
3条回答

您需要在语句中将“=”替换为“=”:

if partychoice = R:"

“=”是赋值运算符
“==”是一个相等运算符

例如

#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

一定要在以后的文章中使用信息丰富的标题,一些与你要问的问题相关的标题。你知道吗

对于条件您需要使用比较运算符==,而不是赋值运算符=。你知道吗

看看这个:https://www.tutorialspoint.com/python/python_basic_operators.htm

希望有帮助!你知道吗

将线路改为

if partychoice == 'R':

首先需要使用两个“=”字符。一个“=”设置变量,两个比较相等。你知道吗

其次,要将变量partychoice与字符串“R”进行比较,因此需要引号。如果没有引号,它会认为您正在将一个引用与另一个对象进行比较。你知道吗

相关问题 更多 >