2017-04-02 79 views
2

新程序員,在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.' 

謝謝大家。

+0

它應該是''==而不是'=',即'如果partychoice ==「R'',同去的嵌套'if's –

+4

修改標題。這與你的問題無關 – Noorul

+0

另外:如果你是一個新的程序員,你應該從Python 3開始。 – DSM

回答

0

您需要的語句來代替 「=」, 「==」:

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 

請務必在將來的帖子中使用信息標題,這些內容與您提問的問題有關。

2

行更改爲

if partychoice == 'R': 

首先,你需要使用兩個 '=' 字符。一個'='設置一個變量,兩個比較是否相等。

其次要變量partychoice比較字符串「R」,所以你需要引號。沒有引號,它認爲你正在比較對另一個對象的引用。

0

有幾個問題與代碼:

  1. 您正在使用賦值運算符,=,而不是比較操作,==
  2. 你需要記住使用'含和比較字符串時,您正在將其與可變R代替字母'R'
  3. 我刪除了raw_input函數內superfulous str()轉換比較。這是不需要的,因爲""定義了一個字符串。
  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.' 
    
+0

謝謝大家! – Russ

+0

嘿,如果你可以標記爲解決方案,它會幫助很多。 (點擊綠色勾號) – Neil