Madlibs Project - Beginner Python

I am currently working on a madlibs project which runs from command line. The code runs ok, but I would like it to raise an error when an integer is inputted instead of a string. I’ve tried looking at different tutorials online, but none of them seem to be helping me in this instance.

The below is the code which currently runs:

noun = str(input("Noun: "))
verb = str(input("Verb: "))
adjective = str(input("Adjective: "))
verb2 = str(input("Verb: "))

class madlibs():
    def first_method():
        madlib = f"Learning to {verb} is very {adjective}. {noun} can be difficult, so you need to {verb2} hard."
        print(madlib)

    def second_method():
        madlib2 = (" Learning to " +   verb)
        print(madlib2)
        
    def third_method():
        madlib3 =  "Learning to {} is very {}. {} can be difficult, so you need to {} hard.".format(verb,adjective,noun,verb2)
        print(madlib3)

call_class = madlibs()
madlibs.first_method()
madlibs.second_method()
madlibs.third_method()

I would appreciate any suggestions :slightly_smiling_face:

Hey There,

Modified Code:

# While True for making sure madlib class's user defined functions are called if all the values are string

while True:
    
    noun = str(input("Noun: "))    
    verb = str(input("Verb: "))
    adjective = str(input("Adjective: "))
    verb2 = str(input("Verb: "))

    # A new list created with all the input values
    str_list={noun,verb,adjective,verb2} 

    # Test code to check the values entered
    print(str_list)

    # "isdigit()" checks if the value is a digit or a string and "all()" checks whether the given condition is satisfied by all elements or not
    y=all(i.isdigit()==False for i in str_list)

    # if-else loop, if all values are satisfied we will call the user defined functions present in class "madlibs()"

    if y ==True:    
        call_class = madlibs()
        madlibs.first_method()
        madlibs.second_method()
        madlibs.third_method()
        break
    # In case even a single input is a digit, we will request user to input correct values.   
    print("Enter the correct values!!")
    
        

class madlibs():
    def first_method():
        madlib = f"Learning to {verb} is very {adjective}. {noun} can be difficult, so you need to {verb2} hard."
        print(madlib)

    def second_method():
        madlib2 = (" Learning to " +   verb)
        print(madlib2)
        
    def third_method():
        madlib3 =  "Learning to {} is very {}. {} can be difficult, so you need to {} hard.".format(verb,adjective,noun,verb2)
        print(madlib3) 

In order to make sure that only string value is passed to the UDF, following steps were taken:

  • All 4 input values taken from the user are added to a list

  • Once the values are added to a list we can check if they are integers or not with the help of “isdigit()” and " all() " method.

y=all(i.isdigit()==False for i in str_list)
  • In the above code all the values of the list are checked for digits, if there is no digit present in all the 4 elements, it will return True which is passed to a variable named “y

  • If loop then checks for the value of “y” variable and calls UDF when True.

  • In case of digits, condition will not satisfy and while loop will ask user to input correct values once again.

  • The loop continues unless all 4 values are strings.

  • At last, the print function of UDF will display result on the screen.

I’ve added comments within the code wherever additional lines were written.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.