I have created this code so far...
def print_slow(str):
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.005)
def menu():
print_slow("-------------[MENU]-------------")
print(" ")
print_slow("1) Enter a sentence.")
print(" ")
print_slow("2) Find the position of a word.")
print(" ")
print_slow("--------------------------------")
print(" ")
print_slow(">>> ")
choice = str(input(" "))
print(" ")
time.sleep(0.5)
if choice == "1":
option1()
if choice == "2":
option2()
def option1():
print_slow("Enter sentence")
sentence = str(input(": "))
print(" ")
menu()
def option2():
if not sentence:
print_slow("Please enter a sentence first!")
time.sleep(0.5)
print(" ")
else:
sentenceUppercase = sentence.upper()
[code goes on...]
Basically when I test it, I press option 2 first and it should give the output 'Please enter a sentence first!', which it does.
I then press option 1 in the menu and it should prompt me to input a sentence (I put 'my name is bob' as a test) and it does.
I then pressed option 2 after inputting the sentence and it should continue with my code - instead it gives the error message 'Please enter a sentence first!'
How can I fix this??
sentenceas a global variable? Because the code you're showing us should give an error because in the functionoption2you're checking ifsentenceevaluates to True (or False), but, from your code, thatsentenceseems not to be defined anywhere. In other words,sentenceunderoption1is a local variable and not the same variable as inoption2.sentence = " "right at the start of my code at the first line but the error still comes up @nbro