7

残念ながら、raw_inputは必要な処理を実行していません。私がやろうとしているのは、プロンプトで入力したものをtotPrimes=にすることです。このスクリプトに置き換えるwhile count < totPrimesと機能します。while count < 50プロンプトに50と入力すると、このスクリプトは機能しません。raw_inputは、使用しようとしている関数ではないのではないかと思います。これが私のコードの抜粋です:

testNum = 3
div = 2
count = 1
totPrimes = raw_input("Please enter the primes: ")

while count < totPrimes :
    while div <= testNum :
4

6 に答える 6

12

行う

totPrimes = int(totPrimes)
while count < totPrimes:
    # code

raw_input数値比較を行う前に、整数または浮動小数点数に変換する必要のある文字列を提供します。

于 2011-04-23T07:39:05.927 に答える
0

totPrimesを次のようなintにキャストする必要があります。

integer = int(totPrimes)
于 2011-04-23T07:40:48.893 に答える
0

生の入力を整数に変換する必要があります。コードについては、コードを次のように変更するだけです。

testNum = 3
div = 2
count = 1
totPrimes = raw_input("Please enter the primes: ")
totPrimes=int(totPrimes)
while count < totPrimes :
    while div <= testNum :
于 2014-04-04T04:19:58.603 に答える
0

raw_input関数は常に「string」タイプのraw_inputdocsを返すため、この場合はtotPrimes「string」タイプを次のように「int」または「float」タイプに変換する必要があります。

totPrimes = raw_input("Please enter the primes: ")
totPrimes = float(totPrimes)

次のように組み合わせることができます。

totPrimes = float(raw_input("Please enter the primes: "))

Pythonで物事を比較するにcount < totPrimesは、比較に意味がある必要があります(数値と数値、文字列と文字列)。そうしないと、プログラムがクラッシュし、while count < totPrimes :whileループが実行されません。

プログラムを保護するためにtry/exceptを使用できます。例外の管理

「ProgrammingforEverybody」コースを受講している人は、数時間でこの方法で評価できます。if/elseステートメントを理解する必要があります。

于 2016-11-29T23:49:48.960 に答える
0

すべての数値を「hrs」または「rate」に変更する必要があります。

例:40*10.50+(h-40)*10.50*1.5間違っている、40*r+(h-40)*r*1.5正しい。

于 2017-06-29T08:52:58.737 に答える
-1

次に入力を使用します。

生の入力は文字列を返します。

inputはintを返します。

于 2016-08-21T18:38:26.350 に答える