2017-04-02 14 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:" 

"=" は代入演算子
ある "==" は等価演算子であります

#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': 

にラインあなたは2 '=' 文字を使用する必要がありますまず。 1つは '='は変数を設定し、2つは等価を比較します。

次に、変数partychoiceと文字列 "R"を比較したいので、引用符が必要です。引用符を付けないと、参照を別のオブジェクトと比較していると考えられます。

0

コードにはいくつかの問題があります。

  1. あなたが代わりに比較演算子、==の代入演算子、=を使用しています。
  2. あなたは現在、あなたの代わりに私がraw_input関数内superfulous str()変換を削除手紙'R'
  3. の変数Rとそれらを比較している、文字列を含むと比較するとき'を使用することを覚えておく必要があります。 ""は文字列を定義するため、これは不要です。
  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

関連する問題