Python 2 while loop fails in second iteration

Hamletta :

I created a program that asks the user for a temperature guess (temperature of space), then calculates a black body spectrum for this temperature and returns the root mean square deviation vs measured data. The user is then asked to guess again:

Here's the code typed, since I can't embed images yet:

while True:
  temperature_guess = input("What is the temperature of the black body spectrum of space (in kelvin)?\
  Type 'stop' when you're happy with your estimate.")

  if temperature_guess == "stop":
    break

  else:
    T_guess = float(temperature_guess)

    print T_guess

    # Calculate intensities produced from measured wavelengths and user guess temperature

    intensities_guess=radiation(measured_lambda_metres,T_guess)

    print intensities_guess

    # Calculate root mean square deviation from measured values

    rmsd = rmsd(measured_intensity, intensities_guess)

    print "For your guessed temperature of" , T_guess , "kelvin, \
    the root mean square deviation from measured intensity data is" , "%.4g" %rmsd , \
    "to 4 significant figures."

The program runs fine for one iteration (one temperature guess), breaks nicely if I tell it to stop, but if I input another temperature, it stalls halfway through. I can see from the print commands that it accepts the temperature input and calculates the intensities based on that, but fails in the root mean square deviation calculation.

Somehow the "rmsd" function doesn't like what it sees on the second go. Why would it do that? Why stop in the middle?

Blckknght :

You appear to be overwriting your rmsd function by its return value. Use a different variable name in the loop and you won't have that issue:

while True:
    # ...

    rmsd_value = rmsd(...)   # don't do rmsd = ... here, or you overwrite the function!

    # ...

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=408278&siteId=1