我有一些而循環問題。我的目標是如果用戶輸入一個負值3次,while循環就會結束。問題是,當我到達第三條消息時,它並不以「milesLoop」結尾,因爲它應該是 (我嘗試使用一個循環,但我想使用多個循環進行測試) 這可能是一個容易解決的問題,但我卡住了。Python雖然循環沒有結束
這是Python代碼:
__author__ = 'MichaelCorbett'
import sys
print('Michael Corbett converter ')
print('\n')
milesLoop = 1
fhietLoop = 1
gallonLoop = 1
poundsLoop = 1
inchesLoop = 1
while milesLoop == 1:
miles = float(input('What\'s up Will, how many miles do you wish to convert to Kilometers? '))
if miles < 0:
print('This converter does not accept negeative values. Try again!')
miles = float(input('What\'s up Will, how many miles do you wish to convert to Kilometers? '))
if miles < 0:
print('This converter does not accept negeative values. Try again')
miles = float(input('What\'s up Will, how many miles do you wish to convert to Kilometers? '))
if miles < 0:
print('This converter does not accept negeative values. Program is Terminated')
milesLoop = 2
while fhietLoop == 1:
Fheit = float(input('What temperature is it outside in Fahrenheit? '))
if Fheit < 0 and Fheit > 1000:
print('This converter does not accept negeative values.')
Fheit = float(input('What temperature is it outside in Fahrenheit? '))
if Fheit < 0 and Fheit > 1000:
print('This converter does not accept negeative values.')
Fheit = float(input('What temperature is it outside in Fahrenheit? '))
if Fheit < 0 and Fheit > 1000:
print('This converter does not accept negeative values. Program is Terminated')
fhietLoop = 2
while gallonLoop == 1:
gallon = float(input('How many gallons are you trying to convert? '))
if gallon < 0:
print('This converter does not accept negeative values.')
gallon = float(input('How many gallons are you trying to convert? '))
if gallon < 0:
print('This converter does not accept negeative values.')
gallon = float(input('How many gallons are you trying to convert? '))
if gallon < 0:
print('This converter does not accept negeative values. Program Terminated')
gallonLoop = 2
while poundsLoop == 1:
pounds = float(input('How many pounds would you like to convert? '))
if pounds < 0:
print('This converter does not accept negeative values.')
pounds = float(input('How many pounds would you like to convert? '))
if pounds < 0:
print('This converter does not accept negeative values.')
pounds = float(input('How many pounds would you like to convert? '))
if pounds < 0:
print('This converter does not accept negeative values. Program Terminated')
poundsLoop = 2
while inchesLoop == 1:
inches = float(input('How many inches would you like to convert? '))
if inches < 0:
print('This converter does not accept negeative values.')
inches = float(input('How many inches would you like to convert? '))
if inches < 0:
print('This converter does not accept negeative values.')
inches = float(input('How many inches would you like to convert? '))
if inches < 0:
print('This converter does not accept negeative values. Program Terminated')
inchesLoop = 2
# Calculations
kilometers = miles * 1.6
celsius = int((Fheit - 32) * 5/9)
liters = gallon * 3.9
kilograms = pounds * .45
centimeters = inches * 2.54
# Output
print('\n')
print(miles, ' miles is ', kilometers, ' Kilometers')
print('Its is ', celsius, 'Celsius outside.')
print(gallon, ' gallons is ', liters, ' liters')
print(pounds, ' pounds is ', kilograms, ' kilograms')
print(inches, ' inches is ', centimeters, ' centimeters')
哇,你可能想現在就調整你的代碼,爲什麼?因爲它是瘋狂的嵌套,你甚至不能調試你自己的代碼,它包含簡單的'if's和'while's – taesu
這是相當多的代碼與複製負載(如使用每個'if'語句三次)請將其縮小到重現此錯誤的最低代碼。 – Adriaan
當用戶預先輸入正值時,您認爲您的代碼會做什麼? –