Hey y'all, so I made my first script on python yesterday, I faced some peculiar results which I could mostly debug by myself. But there is still two that I have no clue about. So I was interested if any of you techies knew the root of this.
The script is pretty simple, it converts Rupees (Indian currency) to USD.
That is the entire script, and the rest of the script works fine so there's no need to worry about that.
But for the peculiarities here's the part of the script that is misbehaving:
Okay so everything speaks for itself, this part confirms that the user wants to convert his currency. BUT!
1) the "if not confirmation" (under the "are you sure that you want to exit the app") does not work for the first time the user inputs anything. The idea behind it is that it will prevent the user from typing a blank string. (because a blank string is considered as false)
But the first time I type a blank string, instead of the "Hmm, I'm not sure if that's a yes or a no!" I get the "Sorry, I did not recognize what you typed".
So something has gone wrong here, because the input is blank but the script considers the string to be true (not a blank string).
HOWEVER, after that input, typing a blank string will give the required result i.e., "Hmm, I'm not sure if that's a yes or a no!".
My suspicious is that it has to do with \n (newline), so does it? How would I fix this?
2) The script crashes for some reason at a specific point.
After reaching the ValidatingFunction(), when the user hits the "Are you sure that you want to exit the app", typing anything that does not contain y or n should give you "Sorry, I did not recognize what you typed!" and the function is called again.
But, it seems after the first call to the function, it does not call the function again.
Notice that the last line "print('lol')" is executed, so the function is not called.
What am I missing here, what is a workaround and what suggestions can you give me to improve my script? Well I appreciate anybody who read this thread and I hope you have a good day ;)!
Also saying "no" in "Are you sure you want to exit the app" also gives the "I'm sorry" message and the 'lol' message at the end.
The script is pretty simple, it converts Rupees (Indian currency) to USD.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
#imports import sys,time,random,string,re # Values Converting = "No" Failure = "No" # Funtions def HasNumbers(inputString): return any (char.isdigit() for char in inputString) def print_slow( str ,sleeptime = 0.05 ): for letter in str : sys.stdout.write(letter) sys.stdout.flush() time.sleep(sleeptime) # Getting person's name, and making sure it's legit. def UserNameFunction(): global UserName UserName = input () if not UserName: print_slow( "Hmm, well I will need a name.\n Try again!\n" , 0.02 ) UserNameFunction() elif HasNumbers(UserName): print_slow( "\nHuh, what kind of a strange person has a number in their name!\n Try again!\n" , 0.02 ) UserNameFunction() UserNameLength = re.sub(r "[\n\t\s]*" , "", UserName) UserName = re.sub( " +" , " " , UserName) UserName = re.sub(r "[\n\t]*" , "", UserName) if len (UserNameLength) > 18 : print_slow( "\nPlease restrict your name to 18 characters..\n Try again!\n" , 0.02 ) UserNameFunction() if len (UserNameLength) < 3 : print_slow( "\nPlease expand your name to 3 or more characters..\n Try again!\n" , 0.02 ) UserNameFunction() print_slow( "Hello, please enter your full name\n" , 0.020 ) UserNameFunction() ######### print_slow( "\nHello, " + string.capwords(UserName) + "!\n\n" , 0.05 ) print_slow( "Would you like to convert your Rupees into Dollars?" , 0.010 ) print_slow( "\n Please use 'Yes' or 'No' (or any suitable abbreviations)\n\n" , 0.020 ) # Asking for confirmation and validating it def ValidatingFunction(): global Confirmation Confirmation = input () if not Confirmation: print_slow( "Hmm, I'm not sure if that's a yes or a no!" , 0.05 ) print_slow( "\n Try again!\n" , 0.02 ) ValidatingFunction() if 'n' in Confirmation or 'N' in Confirmation: print_slow( "Are you sure that you want to exit the app?\n" , 0.020 ) DeniedConfirmation = input () if 'n' in DeniedConfirmation or 'N' in DeniedConfirmation: Converting = 'Yes' elif 'y' in DeniedConfirmation or 'Y' in DeniedConfirmation: print_slow( "Okay, please feel free to use the app at your will" , 0.07 ) else : print_slow( "\nSorry, I did not recognize what you typed." , 0.05 ) print_slow( "\n Try again!\n" , 0.02 ) ValidatingFunction() ValidatingFunction() if 'y' in Confirmation or 'Y' in Confirmation: Converting = 'Yes' else : print_slow( "\nSorry, I did not recognize what you typed." , 0.05 ) print_slow( "\n Try again!" , 0.02 ) if Converting = = 'Yes' : print_slow( '\nOkay, how much would you like to convert into Dollars?' , 0.010 ) print_slow( '\n Please type a number' ) print_slow( ' (decimals are supported)\n' , 0.010 ) #Converting Rupees to Dollars RupeeAmount = input () try : RupeeAmount = round ( float (RupeeAmount), 2 ) except ValueError: print_slow( '\nPlease type in numbers only!\nTry again!\n' , 0.010 ) RupeeAmount = input () try : RupeeAmount = round ( float (RupeeAmount), 2 ) except ValueError: print_slow( "You're a dummie!" , 0.010 ) Failure = 'True' if Failure = = 'No' : DollarAmount = round (RupeeAmount / 67.53 , 2 ) RupeeAmount = str (RupeeAmount) DollarAmount = str (DollarAmount) print_slow( '\n₹' + RupeeAmount + ' would give you $' + DollarAmount) print ( 'lol' ) |
But for the peculiarities here's the part of the script that is misbehaving:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
def ValidatingFunction(): global Confirmation Confirmation = input () if not Confirmation: print_slow( "Hmm, I'm not sure if that's a yes or a no!" , 0.05 ) print_slow( "\n Try again!\n" , 0.02 ) ValidatingFunction() if 'n' in Confirmation or 'N' in Confirmation: print_slow( "Are you sure that you want to exit the app?\n" , 0.020 ) DeniedConfirmation = input () if 'n' in DeniedConfirmation or 'N' in DeniedConfirmation: Converting = 'Yes' elif 'y' in DeniedConfirmation or 'Y' in DeniedConfirmation: print_slow( "Okay, please feel free to use the app at your will" , 0.07 ) else : print_slow( "\nSorry, I did not recognize what you typed." , 0.05 ) print_slow( "\n Try again!\n" , 0.02 ) ValidatingFunction() ValidatingFunction() |
1) the "if not confirmation" (under the "are you sure that you want to exit the app") does not work for the first time the user inputs anything. The idea behind it is that it will prevent the user from typing a blank string. (because a blank string is considered as false)
But the first time I type a blank string, instead of the "Hmm, I'm not sure if that's a yes or a no!" I get the "Sorry, I did not recognize what you typed".
So something has gone wrong here, because the input is blank but the script considers the string to be true (not a blank string).
HOWEVER, after that input, typing a blank string will give the required result i.e., "Hmm, I'm not sure if that's a yes or a no!".
My suspicious is that it has to do with \n (newline), so does it? How would I fix this?
2) The script crashes for some reason at a specific point.
After reaching the ValidatingFunction(), when the user hits the "Are you sure that you want to exit the app", typing anything that does not contain y or n should give you "Sorry, I did not recognize what you typed!" and the function is called again.
But, it seems after the first call to the function, it does not call the function again.
Notice that the last line "print('lol')" is executed, so the function is not called.
What am I missing here, what is a workaround and what suggestions can you give me to improve my script? Well I appreciate anybody who read this thread and I hope you have a good day ;)!
Also saying "no" in "Are you sure you want to exit the app" also gives the "I'm sorry" message and the 'lol' message at the end.