Home > OS >  Python Loop Unable to interrupt
Python Loop Unable to interrupt

Time:08-05

I'm making a curency converter, time converter and weather app for an year 12 computer sciences project. Im unable to interrupt the loop that is being used for the main menu/location selector. Can anyone help? The code is below.

##This program is intended to help travellers with date and currency conversions ##
##Changelog----->##
##V1 - Include code for Forex Converter, code modified from - https://www.w3schools.in/python/examples/real-time-currency-converter##
##V2 - Implement GUI##
##V2.1 - Implement Multiple Screens GUI##
##V3 - Remove all GUI aspects##
##V3.1 - Create initial loop##

##Import Modules##
from forex_python.converter import CurrencyRates
import time
import datetime
import python_weather
import asyncio



##Opening info##
##V3.1##
global enter
enter = 'GO'
while enter == 'GO':
    print("========================================================================================================================================================================================")
    print("")
    print("Welcome to the Traveller Assisstant. This program is able to help you with currency conversions, date and time conversions and viewing weather details of your destination.")
    print("")
    print("========================================================================================================================================================================================")
    time.sleep(2.5)
    ori = str(input("Please enter your current country: "))
    dest = str(input("Please enter your destination country: "))
    time.sleep(5)
    check = str(input("Are you sure you are in "   ori   ", and would like to go to "   dest   "? ")).upper
           
    if check == 'YES':
        enter = 'STOP'
    elif check == 'NO':
        print("Returning to Location Selector")
        enter = 'GO'
    

            
    

##V1##
##Change Currency##
#cr = CurrencyRates()
#output = cr.convert(entry1, entry2, entry3)
#final = round(output, 2)
#print("THE FINAL AMOUNT IS:", final, c2)

CodePudding user response:

A simple typo, that's all that was wrong.

In this line of code:

check = str(input("Are you sure you are in "   ori   ", and would like to go to "   dest   "? ")).upper

You are attempting to use the method

.upper()

But your fatal flaw is that you forgot the parentheses.

I changed this:

check = str(input("Are you sure you are in "   ori   ", and would like to go to "   dest   "? ")).upper

To this:

check = str(input("Are you sure you are in "   ori   ", and would like to go to "   dest   "? ")).upper()

And the code worked perfectly for me

EXPLANATION

In the original code, check could never be equal to 'YES' or 'NO', because of the typo;

.upper was never recognized as a strings' function and returned with this value:

<built-in method upper of str object at 0x105fec130>

.upper() on the other IS in fact a valid function for a string and returned with this value when it was supplied with the input of 'yes':

YES

CodePudding user response:

If you need to exit from the loop when a condition is met you can achieve that easily by using break. So when your condition is met:

either add bellow enter = "STOP" the statement break or just replace enter = "STOP" for break

    if check == 'YES':
        enter = "STOP"
        break

or

    if check == 'YES':
        break

both should work, I guess you could go with the first answer if you need to keep the state of the variable enter otherwise you could just use the second.

The problem with your original code is that you are missing a parenthesis on the declaration of the upper method in this line:

check = str(input("Are you sure you are in "   ori   ", and would like to go to "   dest   "? ")).upper

instead it should be:

check = str(input("Are you sure you are in "   ori   ", and would like to go to "   dest   "? ")).upper()

if you don't add the parenthesis to upper() it means you are declaring the object method itself not triggering instead what the method actually does. (In this case making the string uppercase)

  • Related