Python Forum

Full Version: Help Newbie Please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, nice to be here, Sorry but I'm a total newbie and I'm stuck on this program. I need help with it. I would just love to have a 'clue' to make this work. I don't wanna give up and i can't go any further in my book till i figure this out. Please and Thank you

age = 16
if age < 2:
    print('You are a baby!')
elif age == 2 and age < 4:
    print('You are a toddler')
elif age == 4 and age < 13:
    print('You are a kid')
elif age == 13 and age < 20:
    print('You are a teenager')
elif age == 20 and age < 65:
    print('You are a adult')
elif age >= 65:
    print('You are a elder')
JJG

instead of using 'and for multiple conditions i tried 'or' and its working now. I'm positive i tried using the 'or' for multiple condition and outcome was the same it just wouldn't work

i can sleep now

thanks JJG
Before getting started, please re-post code using code tags, see: BBCODE
also, when pasting code, use shift-ctrl-v to preserve indentation
First of all - let see why your code does not do what you expect.
When you have multiple conditions with and all of them need to evaluate to True in order the whole expression to evaluate to True.
In your example - age == 13 evaluates to False, thus the entire expression age == 13 and age < 20 evaluates to False
With or at least one of the conditions need to evaluate to True and the whole expression will evaluate to True as well. This is why it works when you replace and with or.
That said your code could be simplified to

age = 16
if age < 2:
    print('You are a baby!')
elif age < 4:
    print('You are a toddler')
elif age < 13:
    print('You are a kid')
elif age < 20:
    print('You are a teenager')
elif age < 65:
    print('You are a adult')
else:
    print('You are a elder')
for something to look at, here's another way this could be done:
whoami = [(65, 'an adult'), (20, 'a teenager'), (13, 'a kid'), (4, 'a toddler'), (2, 'a baby'), (999, 'an elder')]

my_age = int(input('How old are you?: '))

your_title = None

for age, title in whoami:
    if my_age >= age:
        break
    your_title = title
print('You are {}'.format(your_title))
Ty very much for feedback I really appreciate it. I also been trying to figure this out, I am so stuck again trying to add ordinal suffix to numbers. I just can't figure it out. Ive searched and searched the web and what i find is way over my head. Any feedback on this would help so much again. Thank you.
ord_nums = ['1','2','3','4','5','6','7','8','9']
suffix = ['st','nd','rd','th']

for suffix in ord_nums:
    if ord_nums < 2:
        print (str(ord_nums) + 'st')
    elif ord_nums < 3:
        print (str(ord_nums) + "nd")
    elif ord_nums < 4:
        print (str(ord_nums)  + "rd")
    elif ord_nums > 4:
        print (str(ord_nums) + "th") 
        break
With 'if' statements i can get to work.. the book I'm reading want me to use 'if','elif' and 'else' chain and thats where i run into problems. Thanks again
ord_nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
if ord_nums < '2':
    print str(ord_nums[0]) + 'st'
    if ord_nums < '3':
        print str(ord_nums[1]) + 'nd'
    if ord_nums < '4':
        print str(ord_nums[2]) + 'rd'
    if ord_nums < '5':
        print str(ord_nums[3]) + 'th'
    if ord_nums < '6':
        print str(ord_nums[4]) + 'th'
reverse the order, and you can use elif,
ex:
if ord_nums < '6':
    print str(ord_nums[4]) + 'th'
elif ord_nums < '5':
...
ty larz60+ but i couldn't get it to work your way. I will not give up on your way i will figure it out.. this is the way i made it work. ty very much for your guys help
for numbers in (range(1,10)):
    if numbers == 1:
        print (str(numbers)) + 'st'
    elif numbers == 2:
        print (str(numbers) + 'nd')
    elif numbers == 3:
        print str(numbers) + 'rd'      
    elif numbers == 4:
        print str(numbers) + 'th'
    elif numbers == 5:
        print str(numbers) + 'th'
    elif numbers == 6:
        print str(numbers) + 'th'
    elif numbers == 7:
        print str(numbers) + 'th' 
    elif numbers == 8:
        print str(numbers) + 'th'
    else: 
        print str(numbers) + 'th'

this way works as well, I just can't figure it out using " < or > " its driving me nuts
ord_nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
for ord_num in ord_nums:

    if ord_num == '9':
        print str(ord_nums[6]) + 'th'
    elif ord_num == '8':
        print str(ord_nums[6]) + 'th'
    elif ord_num == '7':
        print str(ord_nums[5]) + 'th'
    elif ord_num == '6':
        print str(ord_nums[4]) + 'th'
    elif ord_num == '5': 
        print str(ord_nums[3]) + 'th' 
    elif ord_num =='4':
        print str(ord_nums[2]) + 'rd'
    elif ord_num == '3':
        print str(ord_nums[1]) + 'nd'
    elif ord_num == '2':
        print str(ord_nums[0]) + 'st'
else:
    print('You didnt place')    

ok I got it , this thread can be closed .. heh ty :)
ord_nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
for ord_num in ord_nums:

    if ord_num > '8':
        print str(ord_nums[7]) + 'th'
    elif ord_num > '7':
        print str(ord_nums[6]) + 'th'
    elif ord_num > '6':
        print str(ord_nums[5]) + 'th'
    elif ord_num > '5':
        print str(ord_nums[4]) + 'th'
    elif ord_num > '4': 
        print str(ord_nums[3]) + 'th' 
    elif ord_num > '3':
        print str(ord_nums[2]) + 'rd'
    elif ord_num > '2':
        print str(ord_nums[1]) + 'nd'
    elif ord_num > '1':
        print str(ord_nums[0]) + 'st'
else:
    print('You didnt place')