Okay, so basically I have been given python homework for my computer science lesson and I've got to the point that I have no clue what I'm doing and looking up on the internet only makes it worse. Can someone help me?
I'm stuck on task three which is "calling a function" it says that I need to "Call the ChooseASuit function passing the randomNum and storing the answer in a variable called suit"
any help would be greatly appreciated. Thank you :)
The body of the function ChooseASuit has to be changed. In particular let's see what you have done:
if number == 1:
suit = "Hearts"
if number == 2:
suit = "Spades
if number == 3:
suit = "Diamonds"
if number == 4:
suit = "Clubs"
Doesn't make much sense. Because you check if number is equal to 1, if the evaluation is true, you check if number is equal to 2, then if it's true, if it's equal to 3, if it is again true you check if the number is equal to 4. That's what you're doing. But we all know that a number can't be equal to 1, 2, 3 and 4 at the same time.
The correct way to write it would be:
if number == 1:
suit = "Hearts"
elif number == 2:
suit = "Spades
elif number == 3:
suit = "Diamonds"
elif number == 4:
suit = "Clubs"
Or you could use a switch statement. What I wrote above, means: if number is equal to 1, suit is Hearts, if not, check if it's equal to 2 and so on.
you want to follow this formula Disclaimer: I dont know python syntax but I'm not too shabby at programming
quick n' dirty written in C
function ChooseASuit (char myString)
{
char suite[10] = myString;
Select (suite)
{
case 'Hearts' : //some code to-do if passed in suite is equal to hearts
break;
case 'Spades' : //some code to-do if passed in suite is equal to spades
break;
case 'Diamonds' : //some code to-do if passed in suite is equal to diamonds
break;
case "Clubs" : //some code to-do if passed in suite is equal to clubs
break;
default : some code to-do if passed in suite is incorrect e.g. NOT A VALID
}
}
Should get you off on the right track.
You don't want to do a thousand IF statements. They should only be used when you have a narrow decision. IF ELSE basically. If you have to select from a bunch of potential options then you want to use a Select Case to break it up.
I don't know python at all but from what I can see from your screenshot, all you are going to do is print out the random number. What it sounds like you need to do is call the function "ChooseASuit" and pass it the "randomNum" you have created.
So something like this but you would need to modify your function to return suit. print(ChooseASuit(randomNum))
A very dirty way of doing it if you dont know how to return a value would be to place the print inside of your method "ChooseASuit" and then just call the method and pass through the random number. It wouldn't be wrong but it is frowned upon.
I program in C# mostly but the idea should still be the same.
Lol yeah a guy struggling to define basic if logic is going to deal with importing numpy and using the more exotic features of python. We helped him with the code he presented, on the apparent level he's coding at.
if you are suffering with if/else statements in python while taking college level CS i would suggest you buckle down and study more. figure out how to use if/else, do, while statements
esp if you are on the software engineer/developer route. considering this isnt a very strict language. if you are in high school then disregard.