+ 1
#1 in the US this week, that title will be taken soon lol. How can I incorporate more countries without using more code? "|" ?
5 Respostas
+ 4
Use an array/list.
That would actually reduce the code.
Optionally, you can use a dictionary or some way to determine if the country is 3rd or 1st world from inside the array/list, without multiple if statements.
PS: you should consider using 'elif' when you don't want both if statements to ever both be true.
+ 3
Notice how everything is a 3rd world country except for Poland?
This is due to how your if statements are set up.
if country == "Canada"
# do stuff
if country == Poland
# do stuff
else
# do other stuff
What this does:
if country is Canada, we do stuff.
If country is poland, we do stuff.
else, we do other stuff.
That else statement is only bound to the one if statement for Poland, meaning both if statements are being compared.
In other words If the country is canada, the program will still test if the country is Poland, if it isn't Polanf, the else statement runs.
Meanwhile, had you of done:
if country == "Canada"
# do stuff
elif country == "Poland"
# do stuff
else
# do stuff
If one is true, no others are compared.
So, the else statement will only run if all if/elif conditions are false.
This will now only say the country is 3rd world if you do not enter one of those options.
+ 1
Okay Awesome, I'll start on a version with array/list. Could you elaborate on elif for me? I think I'm missing something but when would I ever don't want both if statements to be true? (I did use it in a portion of the code ie Russia, China, as a test but I forgot to take it out to be consistent)
0
I see, that explains why it unnecessarily printed information about 3rd world. I went in and changed it
0
Okay Faith, I added the list! Go check it out, thanks for the feedback and support!