-2

i took a comp class in my college and its in the real early stages. i was asked to make this little program which plays madlibs and now i cant seem to complete it.

import random
verb=input("Enter a verb: ")    
celebrity= input("Enter name of a celebrity: ")
age=input("Enter an age: ")

while not age==int():
    age=(input("C'mon man! Enter a number please: "))


madlibs=f"Coding is fun as if im {verb}. I feel like im {celebrity} and im just {age} years old"

print(madlibs)

again im really new at this so if you have any feedback how i can write the same code in lesser lines and feedback like that, its highly requested

  • 1
    what are you trying to check as condition to breake the loop? if age is an integer? –  Feb 18 '22 at 17:40
  • *age* will be a string. *int()* returns 0 (integer). *age* can therefore never be equal to zero – DarkKnight Feb 18 '22 at 17:42

4 Answers4

1

I would do something like this

while True:
    try:
        age = int(input("Enter your age: "))
        break
    except ValueError:
        print("ERROR, you must enter a number")

Bear in mind that this will only work if you introduce an integer number, which I think is what you tried to do in your code. If you want to allow decimal numbers @Felix has given an answer using .isdecimal()

  • Why would you do something like this? An answer with an explanation is more valuable than an answer that just gives code. – Pranav Hosangadi Feb 18 '22 at 17:45
  • I think the code is self explanatory. You try to convert the input to an int, if no exception is raised you break the loop, otherwise you inform that you need a number and keep inside the loop –  Feb 18 '22 at 17:46
1

input() function always return a string. It won't be dynamically casted, you can use isdecimal() str method.

If you want to use age as a number don't forget to cast it.

You can replace this part:

while not age==int():
    age=(input("C'mon man! Enter a number please: "))

by:

while not age.isdecimal():
    age=input("C'mon man! Enter a number please: ")

age = int(age) # cast str > int

Furthermore, if you want to check your variable type, age==int() condition is not valid, use isintance function instead.

Félix Herbinet
  • 257
  • 1
  • 8
0

You need to define seperate variables for age_target and age_guess.

Then

While not age_target == age_guess
Trav Broad
  • 91
  • 6
0

Here is a working example:

import random


verb = input("Enter a verb: ")
celebrity = input("Enter name of a celebrity: ")
age = input("Enter an age: ")

while True:
    try:
        int(age)
        break
    except ValueError:
        age=(input("C'mon man! Enter a number please: "))


madlibs=f"Coding is fun as if im {verb}. I feel like im {celebrity} and im just {age} years old"

print(madlibs)

What it does differently: int(age) is trying to convert the string age to an integer. If this is not possible, it will throw a ValueError. We catch that ValueError and prompt the user to try again.

If we don't have the while True (keep looping forever) statement, the flow would be the following:

  1. Checking the age against integer. Fail.
  2. Prompt the user again.
  3. Go on to the madlibs statement.

With the while True loop we will stuck the user to the very same prompt till the int(age) doesn't succeed (or at least fail with another type of exception), than we break, which will exit the loop end procede to the next statement.

wankata
  • 855
  • 4
  • 12