harik2014
Badges: 7
Rep:
?
#1
Report Thread starter 1 year ago
#1
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?
0
reply
Lemur14
Badges: 21
Rep:
?
#2
Report 1 year ago
#2
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
Posted from TSR Mobile
Last edited by Lemur14; 1 year ago
2
reply
harik2014
Badges: 7
Rep:
?
#3
Report Thread starter 1 year ago
#3
(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
Posted from TSR Mobile
Hi,

Thanks for the quick reply. I'm using Python 2.7.1
0
reply
JackMac2904
Badges: 16
Rep:
?
#4
Report 1 year ago
#4
I'm having flashbacks now lmao. Good Luck :laugh:
0
reply
Lemur14
Badges: 21
Rep:
?
#5
Report 1 year ago
#5
(Original post by harik2014)
Hey, thanks for the quick reply. I'm coding using Python,
No problem I'm not familiar with python but I'm sure if you Google python break out of loop it will come up

Posted from TSR Mobile
0
reply
harik2014
Badges: 7
Rep:
?
#6
Report Thread starter 1 year ago
#6
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 I'm not familiar with python but I'm sure if you Google python break out of loop it will come up

Posted from TSR Mobile
0
reply
winterscoming
Badges: 19
Rep:
?
#7
Report 1 year ago
#7
(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
1
reply
Lemur14
Badges: 21
Rep:
?
#8
Report 1 year ago
#8
(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 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

Posted from TSR Mobile
0
reply
harik2014
Badges: 7
Rep:
?
#9
Report Thread starter 1 year ago
#9
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?
0
reply
winterscoming
Badges: 19
Rep:
?
#10
Report 1 year ago
#10
(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/c...auses-on-loops
Last edited by winterscoming; 1 year ago
0
reply
harik2014
Badges: 7
Rep:
?
#11
Report Thread starter 1 year ago
#11
(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.")
0
reply
winterscoming
Badges: 19
Rep:
?
#12
Report 1 year ago
#12
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 '[CODE]' tags)

Also, another likely problem here: What is 'wrongAnswer'? have a look at this logic and read it in plain English:
Code:
if guess2 == wrongAnswer:
  break
The logic translates to:
Code:
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'?
Last edited by winterscoming; 1 year ago
0
reply
harik2014
Badges: 7
Rep:
?
#13
Report Thread starter 1 year ago
#13
Hi thanks for the reply. I will try this again tonight. Sorry about the indentation
0
reply
tiredjpeg
Badges: 10
Rep:
?
#14
Report 1 year ago
#14
If you are using python try something like this
Spoiler:
Show

song = "random song"
win = False
points = 3

for i in range(0,2):
<1>
guess = input("Enter your guess")
if guess == song:
</1>
<2>
print("You win, with", str(points), "points")
win = True
break
</2>
<1>
points = 1
</1>
if win == False:
<1>
print("you lose.")
</1>

(i dont know how to make it keep the tabbing, so <1></1> means one tab and <2></2> means 2 tabs
Last edited by tiredjpeg; 1 year ago
0
reply
harik2014
Badges: 7
Rep:
?
#15
Report Thread starter 1 year ago
#15
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?
Name:  python.PNG
Views: 65
Size:  28.0 KB
0
reply
winterscoming
Badges: 19
Rep:
?
#16
Report 1 year ago
#16
(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.
Last edited by winterscoming; 1 year ago
0
reply
winterscoming
Badges: 19
Rep:
?
#17
Report 1 year ago
#17
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.
Last edited by winterscoming; 1 year ago
0
reply
winterscoming
Badges: 19
Rep:
?
#18
Report 1 year ago
#18
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!
Last edited by winterscoming; 1 year ago
0
reply
harik2014
Badges: 7
Rep:
?
#19
Report Thread starter 1 year ago
#19
(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?
0
reply
harik2014
Badges: 7
Rep:
?
#20
Report Thread starter 1 year ago
#20
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?
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Current uni students - are you thinking of dropping out of university?

Yes, I'm seriously considering dropping out (103)
13.29%
I'm not sure (33)
4.26%
No, I'm going to stick it out for now (238)
30.71%
I have already dropped out (19)
2.45%
I'm not a current university student (382)
49.29%

Watched Threads

View All