[Tutor] Trouble Learning Python

Alan Gauld alan.gauld at yahoo.co.uk
Thu Jan 28 04:25:05 EST 2021


On 28/01/2021 00:17, andy-rhodes wrote:

> 2.3 Add two Boolen variables together to equal 0 and again to equal 1.

First off I've got to say that this is a terrible question. It's
asking you to do something you should never do in practice.
Addition is not a boolean operation. It only works because internally
python implements boolean values as integers! That's a fact
you should just ignore and only use boolean operators on
boolean values, it will help keep you sane!

> Create a variable called zero that adds together two boolean values and returns 0. 

Remember that boolean vales should only be True or False.

So you need to add:
- True to True,
- True to False or
- False to False.

Those are the only valid additions you can perform.

> # Create your variables here
> 
> zero = False
> one = True
> 
> my_age = 35
> your_age = 50

The above both need to be True or False. Nothing else
is a boolean value.

> zero = my_age == your_age

But that's not addition.
It's a comparison which is a valid boolean expression.

"Sensible" additions would be:

zero = False + False
one = True + False

> bill_age = 65
> Msry_age = 65

Again boolean variables should only be True or false
So age would not be boolean.

> one = bill_age == Msry_age
> print (one)

Again this is not addition but comparison.
If this is part of the question then it really is a terrible question.
In fact it looks like it comes from the very distant past when Python
didn't have true boolean types.

> this is my attempt which gets errors

Umm, where?

> 2.4 Let's create a dictionary!
> 
> We'll make a dictionary that contains just a few entries. 
> Remember that a Python dictionary consists of key:value pairs. 
> The keys are like the name of the item and the value tells us 
> something about that item.

> Your dictionary will be have three entries (three keys in this case) 
> and a value for each key. We'll choose types of fruit and how many 
> we have of each. A basic dictionary structure with the name myfruit 
> was created for you using the { } - fill in the rest!

This is more promising.

> # Create a dictionary
> # Delete fruit1, fruit2, num1, num2 and then
> # uncomment and fill in with YOUR VALUES

>  myfruit = {'banana':num1, 'kiwi'num2}
This kind of falls apart though. It's not clear exactly what
you need to do and the single line of code doesn't match
the commentary.

I think they are asking you to assign numeric values to
num1,num2 (and presumably num3 since it says 3 entries,
although it only provides 2!)) Or maybe it started as

myfruit = {fruit1:num1, fruit2:num2, fruit3:num3}

And you need to replace the fruit and number names with
literal values? In which case remember that the key
and value are separated by a colon(:) and the pairs
are separated by commas.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list