Python Forum
Simple script that seems to misbehave?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple script that seems to misbehave?
#1
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.

#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')
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:

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()
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.
Reply
#2
Ugh okay I fixed the script the function was written improperly.. my bad haha.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Python script, path not defined dubinaone 3 2,655 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Need help creating a simple script Nonameface 12 4,429 Jul-14-2020, 02:10 PM
Last Post: BitPythoner
  Simple text to binary python script gmills13 2 2,761 Feb-04-2020, 08:44 PM
Last Post: snippsat
  Made a simple script for android (really simple) but it is not running anddontyoucomebacknomore 2 2,325 Mar-06-2019, 12:19 AM
Last Post: anddontyoucomebacknomore
  First time with Python.. need help with simple script shakir_abdul_ahad 7 5,417 May-06-2018, 09:28 AM
Last Post: killerrex
  help with a simple script juanb007 4 3,567 May-01-2018, 08:06 PM
Last Post: ThiefOfTime
  Simple script writted by a dumb dude, myself mm14ag 2 2,697 Apr-28-2018, 11:48 AM
Last Post: mm14ag
  Need help with a simple AHK script Stabu 0 2,071 Feb-24-2018, 08:27 PM
Last Post: Stabu
  A simple script - looking for feedback glidecode 2 2,640 Feb-20-2018, 11:18 PM
Last Post: glidecode
  Phillips hue simple on script Kimzer 6 4,529 Jan-16-2018, 07:53 AM
Last Post: Kimzer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020