-3

I am trying to make it so when you put in something that is not a normal letter for your name it asks you to try again, but the while loop is not recognising it. It just keeps doing the while loop no matter what. I think it might have something to do with the variable is made insde a define.

Here is my code:

from cgi import test
from re import T
import time
import math

big_nono_numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,]
big_nonosymbols = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "", "=", "+", ":", ";", "{", "}", "[", "]", "'", ">", "<", "/", "?"]
yesyesyesses = ['yes', 'y', 'ye', 'ys', 'k', 'ok', 'sure', 'alright', 'okay']
nononoes = ['no', 'n', 'nah', 'na', 'never', 'i dont', 'not really', 'im good',]
v1 = False
v2 = False
v3 = True

def space():
    print('')
    
def validate1():
    if i2.find('`') >=0:
        v2 = False
    elif i2.find('~') >=0:
        v2 = False
    elif i2.find('!') >=0:
        v2 = False
    elif i2.find('@') >=0:
        v2 = False
    elif i2.find('#') >=0:
        v2 = False
    elif i2.find('$') >=0:
        v2 = False
    elif i2.find('%') >=0:
        v2 = False
    elif i2.find('^') >=0:
        v2 = False
    elif i2.find('&') >=0:
        v2 = False
    elif i2.find('*') >=0:
        v2 = False
    elif i2.find('(') >=0:
        v2 = False
    elif i2.find(')') >=0:
        v2 = False
    elif i2.find('-') >=0:
        v2 = False
    elif i2.find('=') >=0:
        v2 = False
    elif i2.find('_') >=0:
        v2 = False
    elif i2.find('+') >=0:
        v2 = False
    elif i2.find('[') >=0:
        v2 = False
    elif i2.find('{') >=0:
        v2 = False
    elif i2.find(']') >=0:
        v2 = False
    elif i2.find('}') >=0:
        v2 = False
    elif i2.find('|') >=0:
        v2 = False
    elif i2.find(';') >=0:
        v2 = False
    elif i2.find(':') >=0:
        v2 = False
    elif i2.find("'") >=0:
        v2 = False
    elif i2.find('"') >=0:
        v2 = False
    elif i2.find(',') >=0:
        v2 = False
    elif i2.find('<') >=0:
        v2 = False
    elif i2.find('.') >=0:
        v2 = False
    elif i2.find('>') >=0:
        v2 = False
    elif i2.find('/') >=0:
        v2 = False
    elif i2.find('?') >=0:
        v2 = False
    elif i2.find('0') >=0: 
        v2 = False
    elif i2.find('1') >=0:
        v2 = False
    elif i2.find('2') >=0:
        v2 = False
    elif i2.find('3') >=0:
        v2 = False
    elif i2.find('4') >=0:
        v2 = False
    elif i2.find('5') >=0:
        v2 = False
    elif i2.find('6') >=0:
        v2 = False
    elif i2.find('7') >=0:
        v2 = False
    elif i2.find('8') >=0:
        v2 = False
    elif i2.find('9') >=0:
        v2 = False
    else:
        v2 = True
    print(v2)
    
def test():
    print(1)
    
chatbot_run = True


space
print('Welcome to Astronaugt-for-hire!')
time.sleep(1)
i1 = input('Do you want to hire an astronaught? ')
if i1 in yesyesyesses:
    v1 = True
    space
    time.sleep(3)   
    print("That's Great! First you will have to create an account.")
elif i1 in nononoes:
    v1 = True
    time.sleep(2)
    print('WELL TOO BAD! YOU ARE ANYWAY. Time to make an account.')
while v1 == False:
    i1 = input('Could you say that again? ')
    if i1 in yesyesyesses:
        v1 = True
        time.sleep(3)
        print("That's Great! First you will have to create an account.")
    elif i1 in nononoes:
        v1 = True
        time.sleep(1)
        print('WELL TOO BAD! YOU ARE ANYWAY. Time to make an account.')

time.sleep(1)
print('Lets start.')

time.sleep(1.5)
i2 = input('Step 1. What is your name? ')
validate1()
test()

while v2 == False:
    i2 = input('please try again')
    validate1()

this is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me postthis is so it lets me post

  • 1
    Your `validate1` set a local `v2`, it never made it out to the global `v2`. Try adding a `global v2` right after `def validate1()` and see if it resolves your issue. – metatoaster Jun 16 '22 at 03:19
  • Please don't pad your question with nonsense. If you have too much code for the text, it may mean you should increase the text meaningfully, or it may mean you should construct a [example] (focus on "minimal"). – Amadan Jun 16 '22 at 03:20

1 Answers1

0

I think it might have something to do with the variable is made insde a define.

Yes. Any assignment inside a function marks the variable as local. Use global v2 inside the function to explicitly mark it otherwise.


Also, there are easier ways to check if some characters are present or absent in a string: re.search, set intersection, all(ch in okay for ch in text)...

And just space all by itself on a line does not do anything in Python, you want space().

Amadan
  • 191,408
  • 23
  • 240
  • 301