2016-11-08 3 views
0

ユーザー入力を受け取り、リストに追加する簡単なコードを作成しようとしています。 「Q」を押して終了するまで、ユーザーはスコアを入力します。何らかの理由で、私のuserInputが文字列でないと思っています。ユーザーが数値を入力するたびに、その入力を整数に変換する必要があります。しかし、それは私にそれをさせません "それは"上を持つことはできません。私はこれを前に他のプログラムでやったので、私は今なぜ私を許さないのか分からない。プログラムは私に属性エラーを与え続けます。 'int'には属性 'upper'がありません

def main(): 
    list1= [] 
    userInput= input("Please enter an integer between 0 and 10: ") 
    amountOfscoresEntered= 0 
    while userInput.upper() != "Q": 
     userInput= int(userInput) 
     amountOfscoresEntered= amountOfscoresEntered + 1 
     if userInput < 0 or userInput > 10: 
      print("Invalid Input") 
     else: 
      list1.append(userInput) 
      totalNumofPointsearned= sum(list1) 
main() 
+0

を。したがって、 'userInput'を' int'にキャストしています。エラーが現れているので、 'int'は' upper'メソッドを持っていません。 'upper'メソッドは*文字列*メソッドです。 – idjaw

+0

また、コードは永遠に続くと思われます。 'userInput'は' while'ループの中で決して "Q"にはなりません。 – idjaw

+0

明確にするために: 'while'ループを初めて入力するとき、' userInput'は実際には文字列ですが、その後は 'int'です。 'userInput'を決して変更しないので、whileループは決して終了せず、' userInput'は 'int'のままです。 whileループの中には、最後の近くに 'input()'関数が必要です。 – Evert

答えて

0

一瞬のためにこれについて考えてみよう。ラインで

userInput= input("Please enter an integer between 0 and 10: ") # input always gives string, even if they enter a number. 

ユーザーが文字列「10」(インプット()メソッドは、常にあなたの文字列を与えるので)その後、最初に入った場合あなたのwhileループは大文字にしようとしています

while userInput.upper() != "Q": 

それで、大文字の「10」はどのように見えますか?あなたのwhileループの最後の行として

1

、これはあなたの問題を解決する必要があり

userInput= input("Please enter an integer between 0 and 10: ")

を追加します。

whileループを使用していて、ユーザーが文字Qを入力したときにそれを中断する条件を指定しています。つまり、ユーザーが複数の入力を続けておきたいということです。これまでのところ、あなたのプログラムでは、ユーザー入力は1回だけです。したがって、whileループの2回目の反復では、にキャストした変数で条件userInput.upper() != "Q"をチェックします。これはあなたのエラーを取得する場所です。ループの最後に別のユーザー入力を行う行を追加すると、問題が解決されます。

1

whileループの繰り返しごとに、キャストuserInputを整数に変換します。整数型は属性.upper()を持たないため、Pythonはそれに応じてエラーを発生させます。

目標を達成するには、代わりにwhile Trueループを使用することをお勧めします。そうすれば、あなたはwhileループにユーザー入力をテスト避けるため、そして自分が.upper()を使用することができます:

def main(): 
    list1 = [] 
    amountOfscoresEntered = 0 

    while True: # use while true instead 

     # get user input repeatedly 
     userInput = input("Please enter an integer between 0 and 10: ") 

     # lets check the user input 
     # before we cast it to an integer. 
     if userInput.upper() == "Q": 
      break 

     elif 0 < int(userInput) < 10: 
      print("Invalid Input") 

     else: 
      # once we have verified that 
      # our input is what we want, 
      # we can cast `userInput` to 
      # an integer, and add it to 
      # `list1`. 
      list1.append(int(userInput)) 
      amountOfscoresEntered += 1 

    totalNumofPointsearned = sum(list1) 
0

が作る偽のループをループの条件のための変数を使用してみてください、その後、とき

if userInput.isalpha(): 
    if userInput.upper() == "Q": 
     running = False

0

私はあなたが探している解決策を提供する前に留意すべきことがいくつかあります。

  1. スタイルに関しては、コード全体にわたって一貫性を持たせてください。
  2. Pythonバージョン:使用しているPythonのバージョンによっては、この種のエラーが発生する場合もあります。

    input() - > python2.xは文字をうまく解釈しません。変数を変数に変換し、変数を解釈しようとします。したがって、プログラムが失敗する原因になります。

    raw_input() - >は実際に入力を受け取り、必要な処理を実行できます。

  3. 固定コード:ユーザ入力がwhileループの内側であった

    def main(): 
        list1 = [] 
        amountOfScoresEntered = 0 
        totalNumOfPointsEarned = 0 
        userInput= raw_input("Please enter an integer between 0 and 10: ") 
        while str(userInput).upper()!="Q": 
    
         amountOfScoresEntered= amountOfScoresEntered + 1 
    
         if int(userInput) < 0 and int(userInput) > 10: 
          print("Invalid Input") 
         else: 
          list1.append(userInput) 
          totalNumofPointsEarned= sum(list1) 
    
         userInput= raw_input("Please enter an integer between 0 and 10: ") 
    main() 
    
  4. input() and raw_input() reference

  5. Input and Output

  6. raw_input()

0

それに応じてtryブロックを使用して例外をキャッチする必要があります。 upper()は整数では使用できないので、 'Q'と 'q'の両方を個別に検証に使用しています。

コードの作業: `int型(ユーザ入力)`:あなたはこれをやっているあなたのwhileループの内側

def main(): 
list1 = [] 
amountOfscoresEntered = 0 
totalNumofPointsearned=0 
while True: 
    userInput = input("Please enter an integer between 0 and 10: ") 
    try: 
     if userInput == "Q" or userInput == 'q': 
      break 
     elif not userInput.isalpha() and not int(userInput) < 0 and not int(userInput) >10: 
      userInput = int(userInput) 
      amountOfscoresEntered = amountOfscoresEntered + 1 
      list1.append(userInput) 
      totalNumofPointsearned = sum(list1) 
     else: 
      print("Invalid Input") 
    except ValueError: 
      print ('Invalid Input') 
return amountOfscoresEntered, totalNumofPointsearned 


if __name__ == '__main__': 
    attempt, total_point_earned = main() 
    print('Number of Attempts=%s \nTotal Points Earned=%s' %(attempt,total_point_earned)) 
+0

この回答が現在の問題を解決する際にOPを助ける方法についての説明をいくつか追加してください –

関連する問題