私は、携帯電話プランでは、ユーザーが月額$ 20で使用できる400分を持っているプログラムを作成しています。その後、ユーザーが月に400分以上使用すると、400分を超えて1分あたり5セントが課金されますプランの分数今月使用した分数をユーザーに尋ねてから、請求額を計算します。その人が負の整数を入力したかどうかを必ず確認してください(次に、「負の数を入力しました」と出力する必要があります)。
私のコード:
def main():
# the bill will always be at least 20
res = 20
# again is a sentinel
# we want the user to at least try the program once
again = True
while again:
minutes = int(input("How many minutes did you use this month? "))
# error correction loop
# in the case they enter negative minutes
while minutes < 0:
print("You entered a negative number. Try again.")
# you must cast to an int
# with int()
minutes = int(input("How many minutes did you use this month? "))
# get the remainder
remainder = minutes - 400
# apply five cent charge
if remainder > 0:
res += remainder * 0.05
print("Your monthly bill is: ","$",res)
det = input("Would you like to try again? Y/N: ")
again = (det == "Y")
main()
600と入力すると、正解は$ 30になります。もう一度行くように求められたら、「はい」の場合はYと入力し、500などの低い値を入力すると、$ 35という答えが返されます。これは意味がありません。また、yと入力して下の値を入力すると、価格が上がります。分が下がると価格が上がるように見えますが、分が上がると価格が上がるはずです。
私は何が間違っているのですか。そして、お時間をいただきありがとうございます。
res
ループ内に移動する必要があるため、ループがリセットされます。このような:
#!/usr/bin/env python3.6
def main():
# again is a sentinel
# we want the user to at least try the program once
again = True
while again:
res = 20 # Reset this variable
minutes = int(input("How many minutes did you use this month? "))
# error correction loop
# in the case they enter negative minutes
while minutes < 0:
print("You entered a negative number. Try again.")
# you must cast to an int
# with int()
minutes = int(input("How many minutes did you use this month? "))
# get the remainder
remainder = minutes - 400
# apply five cent charge
if remainder > 0:
res += remainder * 0.05
print("Your monthly bill is: ", "$", res)
det = input("Would you like to try again? Y/N: ")
again = (det == "Y")
main()
あなたがそれを持っていた方法は、res
永遠に増加し続け、20にリセットされることはありませんでした。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加