The Student Room Group

computer science programming project

For my programming project, i am making a music quiz which needs to randomly generate a song - and the user has two guesses to guess the song.

If it is correct the first time, they get 3 points. If they guess it wrong, they get another try. If the guess is correct the second time, they get 1 point. If it is wrong again, game over. This needs to loop.

I've managed to randomly generate the song - but what I cannot work out is how to use the if statement and loop. I was wondering if anyone knew how to do this - if so, how?

Scroll to see replies

I don't know what language you're working in but I think the thing you're missing is a break.
So you have the for loop
For X=0 to whatever
Play random song
Take in first guess
If correct guess first {
Add three points
Else{
Take in second guess
If correct guess second{
Add one point}
Else {
Output "Game over"
break}
End if
End if
Next X

Type thing
The break should take it out of the loop at whatever stage it is at :smile:
Posted from TSR Mobile
(edited 5 years ago)
Reply 2
Original post by Lemur14
I don't know what language you're working in but I think the thing you're missing is a break.
So you have the for loop
For X=0 to whatever
Play random song
Take in first guess
If correct guess first {
Add three points
Else{
Take in second guess
If correct guess second{
Add one point}
Else {
Output "Game over"
break}
End if
End if
Next X

Type thing
The break should take it out of the loop at whatever stage it is at :smile:
Posted from TSR Mobile

Hi,

Thanks for the quick reply. I'm using Python 2.7.1
I'm having flashbacks now lmao. Good Luck :laugh:
Original post by harik2014
Hey, thanks for the quick reply. I'm coding using Python,

No problem :smile: I'm not familiar with python but I'm sure if you Google python break out of loop it will come up :smile:

Posted from TSR Mobile
Reply 5
Hi, that's ok. So can I still use the same format that you showed me but with the commands in Python?

Original post by Lemur14
No problem :smile: I'm not familiar with python but I'm sure if you Google python break out of loop it will come up :smile:

Posted from TSR Mobile
Original post by harik2014
Hi, that's ok. So can I still use the same format that you showed me but with the commands in Python?

The kind of logic that @Lemur14 has written above would work for any programming language -- Yes, the syntax is different, but fundamentally all programming languages have the ability to do things like while, if, else, break, etc. Logic and pseudocode are neutral/agnostic to programming languages

The thing to do is to try it and see what happens. If you're using Python with IDLE or PyCharm or some other IDE, then make sure you take a few minutes to learn about breakpoints and the debugger -- particularly how to use those to step through code, and inspect variables so that you can see/understand what's happening and troubleshoot problems! the debugger is really, really useful for helping you see exactly what your program is doing, which will save you loads of time if you end up getting stuck with some code you've written :smile:
Original post by harik2014
Hi, that's ok. So can I still use the same format that you showed me but with the commands in Python?

Yep you can use the format :smile: I think it should do what you asked for, although obviously some testing would be a good idea! It's effectively a loose form of pseudo code so converting it to python shouldn't be too hard with the help of Google if necessary :smile:

Posted from TSR Mobile
Reply 8
hi, thank you. I tried using break and for in python, and researched this. But I get an error saying 'break' outside loop. Do you know why this might be the case?
Original post by harik2014
hi, thank you. I tried using break and for in python, and researched this. But I get an error saying 'break' outside loop. Do you know why this might be the case?

You can only use the break keyword inside a loop (the only purpose of 'break' is to terminate a loop) -- check your indentation carefully, Python is very sensitive to the whitespace/formatting of your code

Have a look at this example:
https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops
(edited 5 years ago)
Reply 10
Original post by winterscoming
You can only use the break keyword inside a loop (the only purpose of 'break' is to terminate a loop) -- check your indentation carefully, Python is very sensitive to the whitespace/format

Ok. Below is my code, do you know what to suggest? Sorry I do not know how to format it properly!

for x in range(3):
x = x+1

if guess == correctAnswer:
print("Well done. You scored three point!")
userScore =+ 3

else:
guess2 = input("Nope. Try again.")
nb_tries_left -= 1

I have defined correctAnswer and wrongAnswer earlier in the program

if guess2 == wrongAnswer:
break
print("Sorry, that's again incorrect. Come again soon.")
The forum has removed your indenting/formatting, so it's impossible to tell from your post whether the formatting of your code is a problem. You could try this site instead to create a link: https://repl.it/languages/python3 (TSR loses indents unless you use '' tags)

Also, another likely problem here: What is 'wrongAnswer'? have a look at this logic and read it in plain English:

if guess2 == wrongAnswer:
break

The logic translates to:

IF the data in variable 'guess2' is equal to the data in variable 'wrongAnswer' then
break

So, if the data 'guess2' is anything other than an exact match with the 'wrongAnswer' variable then your loop won't break.

if your goal is to compare the user's input with 'correctAnswer', then you need to be checking your input against 'correctAnswer' instead. (Although perhaps you intended to use 'not-equal' rather than 'is-equal-to'?
(edited 5 years ago)
Reply 12
Hi thanks for the reply. I will try this again tonight. Sorry about the indentation
If you are using python try something like this

Spoiler


(i dont know how to make it keep the tabbing, so <1></1> means one tab and <2></2> means 2 tabs
(edited 5 years ago)
Reply 14
correctAnswer = (guess or guess2 == random_song)

wrongAnswer = (guess or guess2 !=random_song)

So do I just need to change where I put break in the for loop?
python.PNG
(I split this post up into separate posts because for some reason the forum was complaining at me...)

Your code has a lot of different problems - quite a few things which aren't going to work quite the way you're probably expecting.

Firstly, your 'else' isn't indented correctly -- the indentation level of 'else' must match the indentation level of its corresponding 'if' otherwise it won't work. (I'm surprised that Python hasn't complained about that?)
Remember that indentation forces the flow of your program - so everything you want to be repeated inside your 'for' loop must be indented to a deeper level than the 'for' keyword. Anything which is at the same level as the 'for' keyword will happen outside of the loop.

secondly , 'correctAnswer' and 'wrongAnswer' are just plain, simple 'data' variables. The = symbol in programming isn't the same as the = symbol in maths.

- In maths, the = symbol means "equals" and you can use it to create a function. e.g. f(x) = x + 1 is a mathematical function
- In programming, the = symbol means "is assigned to", and you use it to store something (usually data) into memory.

So the following code:
correctAnswer = (guess or guess2 == random_song)
means this in plain English (you need to start from the right-hand side of the = symbol to make sense of it):
- 1: calculate the result of (guess or guess2 == random song)
- 2: store the result in an area of memory, called correctAnswer

In other words, correctAnswer doesn't hold a calculation -- correctAnswer doesn't even know anything about 'guess' or 'guess2' -- it simply stores a value in memory. The same goes for your wrongAnswer variable.
(edited 5 years ago)
Secondly, the boolean 'or' operator will behave in a way which might surprise you:
correctAnswer = (guess or guess2 == random_song)
In plain English it means

- 1: if 'guess' does not have a "falsey" value such as an empty string, then assign the content of the 'guess' variable to the 'correctAnswer' variable
- 2: if 'guess' does have a "falsey" value, then assign the result of (guess2 == random_song) to the 'correctAnswer' variable.

So, depending on whether 'guess' is an empty string it will either behave like this:
correctAnswer = guess
or this
correctAnswer = (guess2 == random_song)

There are two basic problems with all of this, and it's because boolean logic cannot compare multiple items. In other words it is not possible to use boolean logic to compare both your 'guess' and 'guess2' with the 'random_song' variable -- you need to handle them separately.

Python treats 'guess' as an individual expression, and checks whether its "truthy" or "falsey" -- if it's truthy, then all your code is doing is getting the content of guess. I'm assuming this is almost certainly not what you're expecting to happen.


I would recommend that you get rid of 'correctAnswer' and 'wrongAnswer' altogether because you don't actually need them anyway, and they're causing loads of problems. Actually, given the structure of your program, you don't need 'guess2' either.
Remember that you can re-use the same variable many times, so if the user gets their first guess wrong, you can overwrite 'guess' with another input() and reuse the same variable for their next guess.

The mext thing to do is to change your 'if' logic so that you're creating boolean statements which simply compare two variables with each other. for example:
if guess == random_song:

You can do this multiple times if you need to -- if you change your code to overwrite 'guess' after they get their first attempt wrong, then the content of 'guess' will be different the next time.
(edited 5 years ago)
I would very strongly suggest that you start a completely new program from scratch to solve individual problems in smaller programs on their own because there are too many things to fix at once.

Try to break this problem down into much smaller pieces so that you can solve them one-by-one, and also so that you are focused on getting to grips with smaller parts of the language. As a general rule (especially when learning) you should never write a big chunk of code all at once. Always write very small, simple pieces of code and test each part thoroughly. The key to programming is "divide and conquer"

The other issue is that you seem to be having a lot of trouble with things like 'for', 'if', and 'else', so trying to solve individual problems on their own will be much easier and should help you see what's happening more clearly.


For example - Start out writing a program which only asks the user once and doesn't use a 'for' loop, but tells them whether the answer is right or wrong -- make sure this program is 100% working before doing anything else.

Secondly, try writing a program which just demonstrates a basic loop to ask a user mulitple times and simply writes out whatever the user has said to the screen just so that you can see what's happening with loops and reusing the same variable.
-- https://wiki.python.org/moin/ForLoop
-- https://www.w3schools.com/python/python_for_loops.asp

Thirdly, try combining combining those programs to ask the user multiple times inside a loop, but which doesn't attempt to calculate any kind of score. Again, make sure that program is 100% working before doing anything else. Remember that the difference between this and the first program is merely using a loop to repeat the same code multiple times


If you can approach the problem like this, the whole thing might start to make more sense and you should have an easier time being able to see what's happening; it's often the case with programming that if something seems too hard and you don't understand what's happening, then you're over-complicating it and you need to start with something simple until you understand what's actually happening!
(edited 5 years ago)
Reply 18
Original post by winterscoming
I would very strongly suggest that you start a completely new program from scratch to solve individual problems in smaller programs on their own because there are too many things to fix at once.

Try to break this problem down into much smaller pieces so that you can solve them one-by-one, and also so that you are focused on getting to grips with smaller parts of the language. As a general rule (especially when learning) you should never write a big chunk of code all at once. Always write very small, simple pieces of code and test each part thoroughly. The key to programming is "divide and conquer"

The other issue is that you seem to be having a lot of trouble with things like 'for', 'if', and 'else', so trying to solve individual problems on their own will be much easier and should help you see what's happening more clearly.


For example - Start out writing a program which only asks the user once and doesn't use a 'for' loop, but tells them whether the answer is right or wrong -- make sure this program is 100% working before doing anything else.

Secondly, try writing a program which just demonstrates a basic loop to ask a user mulitple times and simply writes out whatever the user has said to the screen just so that you can see what's happening with loops and reusing the same variable.
-- https://wiki.python.org/moin/ForLoop
-- https://www.w3schools.com/python/python_for_loops.asp

Thirdly, try combining combining those programs to ask the user multiple times inside a loop, but which doesn't attempt to calculate any kind of score. Again, make sure that program is 100% working before doing anything else. Remember that the difference between this and the first program is merely using a loop to repeat the same code multiple times


If you can approach the problem like this, the whole thing might start to make more sense and you should have an easier time being able to see what's happening; it's often the case with programming that if something seems too hard and you don't understand what's happening, then you're over-complicating it and you need to start with something simple until you understand what's actually happening!

Hi thanks for the comments. I appreciate it . So to summarise, I can use the guess variable several times?
Reply 19
Hi, I tried your solution. I used the guess variable more than once, even for the second guess. However, it printed off both "Well done, you scored three points" and "you scored one point" at once. Do you think the Pythonmachine has confused both again?

Quick Reply

Latest

Trending

Trending