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

时间:2017-04-02 00:51:29

标签: python

新程序员,在python 2.7中工作。

使用此代码,我在行中出现语法错误'如果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.'

谢谢大家。

4 个答案:

答案 0 :(得分:2)

将行更改为

if partychoice == 'R':

首先,你需要使用两个'='字符。一个'='设置一个变量,两个比较相等。

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

答案 1 :(得分:0)

对于 CONDITIONALS ,您需要使用比较运算符==而不是=的赋值运算符。

检查出来:https://www.tutorialspoint.com/python/python_basic_operators.htm

希望有所帮助!

答案 2 :(得分:0)

代码有几个问题:

  1. 您使用的是分配运算符=,而不是比较运算符==
  2. 您需要记住在包含和比较字符串时使用',目前您正在将它们与变量R进行比较而不是字母'R'
  3. 我删除了str()函数中的超级raw_input转换。由于""定义了一个字符串,因此不需要这样做。
  4. 您只需在结果上调用.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.'
    

答案 3 :(得分:0)

您需要在声明中将“=”替换为“==”:

if partychoice = R:"

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

e.g。

#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

请务必在以后的帖子中使用信息性标题,这与您提出的问题有关。