Lesson 11 - Python Programming (Automate the Boring Stuff with Python)

preview_player
Показать описание


Рекомендации по теме
Комментарии
Автор

How to do an error message for negative numbers:

print('How many cats do you have')
numCats = input()
try:
if int(numCats) >= 4:
print('That is a lot of cats')

elif int(numCats) < 0:
print('You can`t enter a negative number')

elif int(numCats) < 4:
print('That is not that many cats')

except ValueError:
print('You did not enter a number')

It was very easy, wich means that you are doing your job of learning me coding very well!

mine-to-go
Автор

Hi AI, I don't know how far I will reach in Python this time, this is my third attempt in the last 1 year to learn Python, and each time I gave up when I reach functions but this time I feel more confident and I am at exception handling.
Your style of teaching is exceptional and interesting. But I like this exception :D.
Thank you so much for the efforts you have put in.

vikramgodara
Автор

This is my variation where I tried to make it loop back when a negative number or non-integer is entered:

while True:
print('how many dogs do you have?')
numdogs = input()
try:
while int(numdogs) <= -1:
print('you own negative number of dogs??')
print('please enter a valid number!')
numdogs=input()
continue
if int(numdogs) >= 4:
print('yo thats a lot!')
elif int(numdogs) == 0:
print('get one you wont regret it!')
elif int(numdogs) < 4:
print('thats awesome!')
break
except:
print('please enter an integer!')
continue

glendonchua
Автор

print('how old are you?')
age = input()
try:
if int(age) >= 18:
print(' you are an adult wlc')
elif int(age) < 0:
print('negative numbers are not allowed')
elif int(age)< 17:
print('you are a kiddo go play with sand')
else:
print('good job you made it')
except ValueError:
print('You did not enter a number.')

builder
Автор

print ("How many cats do you have?")
numCats = input()
try:
if int(numCats) >= 4:
print("That's a lot of cats!")
elif int (numCats) <=0:
print("... What?")
else:
print ("Sweet.")
except ValueError:
print ("Please type a number!")



Started today with the videos - super nice work! :)

llililillilliilli
Автор

I solved your homework task myself , which means you're an awesome teacher heres my code in Dutch.
print('hoveel katten heb jij?')
kat = input()
try:
if int(kat) >= 4:
print ('dat is veel')
elif int(kat) < 0:
print ('dat kan niet')
elif int(kat) == 0:
print('je moet er eentje kopen')
else:
print ('dat is cool')
except ValueError:
print('vul een getal in')
print('einde')

xanhxanh
Автор

Thanks for the lessons Al, really enjoying them. I've been playing round with this code for a couple of hours trying to get it it to go back to the beginning after the except. Finally worked it out! Then decided it didn't display that well so added a couple more bits after some googling.
Would be great if someone can show how this can be done better, here's my attempt anyway:

import time

def cats(numCats):
while True:
try:
if int(numCats) >= 4:
print('Thats a lot of cats!')
break
elif int(numCats) >= 1 :
print('Thats not many cats.')
break
elif int(numCats) == 0:
print('I hate cats too!')
break
elif int(numCats) < 0:
print('Oh! So your the one who has been killing all the cats!')
break
except ValueError:
print('We need a number dude! No fancy floats or strange strings!')
print('\n')
time.sleep(1)
print('Lets try again.')
print('\n')
time.sleep(1)
print('How many cats do you have?')
numCats = input('> ')

print('How many cats do you have?')
numCats = input('> ')
cats(numCats)

billsterno
Автор

print('How many cats do you have?')
numCats = input()
try:
if int(numCats) >=4:
print('That is alot of cats.')
elif int(numCats) <0:
print('You cant enter a negative number')
else:
print('That is not that many cats.')
except ValueError:
print('You did not enter a number')

This Channel Wins.

shabam
Автор

Hi Al... Man I am so glad I had a friend turn me on to your channel. I had started to re-learn Visual Basic that I learned years ago because I want to be able to code behind my Excel work and my friend said, "Why don't you just learn Python? Its easier and I have a great resource for you". Well he was right. Thank you for your great work! I had one quick thought or recommendation for the newbies like myself for this particular lesson. I am still trying to figure out 'syntax' and why things are written a certain way, but since we were dealing with errors in this lesson I thought I would try and create one on purpose by changing the last "print (div42by (1)) to (div42By (1)) to see if it would error out and it did which is what I thought. But, what I was wondering is can you tell me why specifically you have in the def line as (divideBy) and in the print lines you have div42by (one with a cap B and one with a small b) or is this just syntax that is set in the program and I will just get used to it over time? I guess I am maybe looking for a best practices for syntax if you have a resource or description that might help? Thanks again...

MarkRizzoTheDragonWizard
Автор

After googling many terms, versions, wording and such to death on stack over flow, and many theories very proud i ended up solving how to accept numeric or worded input as acceptable input lol. They were saying to always structure ( idk if they were 100% right or not) the input to be accepted as a string. So here it is---->

print("how many zombies have you captured!?!?")
LessThanFour = ('one', 'two', 'three', 'four', '1', '2', '3', '4')
MoreThanFour = ('five', 'six', 'seven', 'eight', 'nine', 'ten', '5', '6', '7', '8', '9', '10')
numZombies = str(input())
if numZombies in LessThanFour:
print("Start trying harder biotch!")
elif numZombies in MoreThanFour:
print("Your zombie army is building up well!")

If you want to network and or collaborate w me look up the FB page name "knowledge obsession"

jakeambrose
Автор

Everybody's alrdy posted a bunch of different solutions to the cat thing but here's mine aswell because this is the first time ever I created code to solve a problem and wasn't just following along a tutorial and I don't care how easy it is I'm still proud of myself hah!

print('How many cats do you have?')
numcats = input('(Write number of cats: )')

try:
if int(numcats) <= 4 and int(numcats) > 0:
print('That is not many cats.')
elif int(numcats) > 4:
print('That is alot of cats.')
else:
print('That is highly unlikely.')
except ValueError:
print('You must write a number.')

GettaGitta
Автор

If anyone is wondering how to add that error for negative numbers, remember when Al taught us about else if, or 'elif' ;)

olivereaton
Автор

For 6:49 (time-stamp); the code I have came up with is:


print('How many cats do you have?')
numCats = input()
try:
if int(numCats) >= 4:
print("That's a lot of cats")
elif int(numCats) > -1:
print("That isn't that many cats compared to >= 4 cats")
if int(numCats) < 0:
print("That isn't a possible value. Maybe you meant 0 cats?")

except ValueError:
print("You didn't type in a value; valueError")

PizzaGodOneMillion
Автор

Here's what I came up with. Worked as expected after testing and messing around a little bit.

print('How many cats do you have?')
numCats = input()
try:
if int(numCats) >= 4:
print('That is a lot of cats.')
if int(numCats) < 0:
print('You cannot have negative number of cats silly')
if int(numCats) < 4 and int(numCats) >= 0:
print('That is not that many cats.')
except ValueError:
print('You did not enter a numeric number.')

Purerhpkr
Автор

I really like it when you give little exercises to add bits to the code!
for anyone wondering, i added
elif int(numCats) < 0:
to catch anything less than 0

hhjikakou
Автор

I added elif to detect less than zero and then prints, "you dont have any cats"

print ("how many cats do you have?")
NumCats = input ()
try:
if int (NumCats) >= 4:
print ("thats a lot of cats!")
elif int (NumCats) <0:
print ("you dont have any cats")
else:
print ("thats not that many cats")
except ValueError:
Print ("you didnt enter a number!!")

TheKcmalcolm
Автор

i think this is a better way to implement the same

def cat():
print('how many cats do you have?')
global cats
cats = input()
try:
if int(cats) >=4:
print('That is a lot of cats')
elif int(cats) <0:
print('Please enter a postive number \n')
cat()
else:
print ('That is not a lot of cats')
except ValueError:
print('Please enter a number')
cat()

cat()

AGRIMJAIN
Автор

The try & error functions will be an important basic for my programs....

ebuyuto
Автор


Code:

while True:
print('How many cats do you have?')
try:
numOfCats = int(input())
while numOfCats < 0:
print('Error: Invalid argument. Please enter a positive number.')
print('Tell me again, how many cats do you have?')
numOfCats = int(input())
if numOfCats >= 4:
print('That is a lot of cats.')
break
elif numOfCats == 0:
print('So you are not a cat person.')
break
else:
print('That is not that many cats')
break
except ValueError:
print('Error: Invalid argument. Please enter a number.')
print('I am going to ask you again.', end=' ')
input('Press any key to exit.')

hirocode
Автор

This was my code for if there was a negative number:

numCats = input("How many cats do you have? ")
try:
if int(numCats) >= 4:
print("That is alot of cats.")
elif int(numCats) < 0:
print("You cannot have negative cats \nwhat is wrong with you?")
else:
print("That is not that many cats.")
except ValueError:
print("You did not enter a number")

rverrrrr