hoocke 3 years ago Help please! I'm stuck with exercise 1.7 and how to decide what the user have written. What´s wrong? if (player1 != "Rock" or "Paper" or "Scissor"): print ("You can only write Rock, Paper or Scissor") inmatning()

• This Question is Open
1. andrew.m.higgs

How about using a while loop?

2. hoocke

How can that help me? I need to write a if-statement checking if the user is writing someting else than Rock, Paper or Scissor...

3. Screech

andrew is correct in that you will need some kind of looping construct like a while loop to return and ask the user to try again to enter valid input. In the snippet you posted, as far as I know, you can't distribute the inequality test across the 'or's. Equality and inequality have higher precedence than the 'or' operation. Even if the language supports it, its easy to misinterpret. Its better to spell it out for anyone reading the code if (player1 != "Rock") and (player1 != "Paper") and (playe1 != "Scissor"): print ("You can only write Rock, Paper or Scissor") I threw in some clarifying parens and changed the conjunction to 'and' which I think is correct rather than 'or'. I have no idea what inmatning() is If there is any question about the syntax, you can always try a quick test in the Python interpreter

4. petew71

@Screech, Are you sure the conjunction shouldn't be " or " ?....As in the player must input one "or" another of the 3 options.

5. andrew.m.higgs

Yip. while player1 != "rock" or player1 != "scissors" or player1 != "paper": ask for user input You would need to use or because otherwise player1 would need to be all 3 values in order to exit the while loop.

6. petew71

@hoocke Exercise 1.7 was just looking at ' if, elif,else' conditionals . So you ask for the input of player1 and player2. then, using if-elif compare them and give an appropriate print return. You can finish off with an else condition that tells the players 'This is not a valid object selection.' @andrew ...Phew, glad i got that right! I was worried i might have missed something! lol

7. andrew.m.higgs

My bad. Sorry. I should have checked the question again. :-)

8. petew71

It's easy to forget what was covered when!... ;0)

9. yptrumpet

It looks like you guys have all the right elements for the logical evaluation, but in the wrong order. As it is, the while loop never terminates, even with the right player1 values. Should be one of the following, which are equivalent: while not(player1 == "rock" or player1 == "scissors" or player1 == "paper"): while (pOne != "rock" and pOne != "scissors" and pOne != "paper"): I don't know about you, but the confusion for me was that we usually write logical statements to see if input PASSES, but here we were checking for failure.

10. yptrumpet

(where pOne was my version of player1, my bad)

11. andrew.m.higgs

You are quite right. Trying to think to quickly. Shame on me....:-)

12. lh4219

Is this similar to what you have next =True while next == True: p1 = input("Player 1 please enter 'scissors', 'paper' or 'rock' ") p2 = input("Player 2 please enter 'scissors', 'paper' or 'rock' ") if (p1 != 'scissors' and p1 != 'paper' and p1 != 'rock') or (p2 != 'scissors' and p2 != 'paper' and p2 != 'rock'): print 'Both players must enter either scissors, paper or rock.' print continue else: if (p1 == 'scissors' and p2 == 'paper' or p1 == 'paper' and p2 == 'rock' or p1 == 'rock' and p2 == 'scissors'): print "Player 1 wins!" next = False elif(p2 == 'scissors' and p1 == 'paper' or p2 == 'paper' and p1 == 'rock' or p2 == 'rock' and p1 == 'scissors'): print "Player 2 wins!" next = False else: print "Players tie" print

13. MicroBot

if pl1=='rock' or pl1=='paper' or pl1=='scissors': do smthing else: ask user to try again. can this help?

14. hoocke

Thx all for the help!

15. lh4219

your welcome any time you have a problem email

16. RichardGoodrich

I just started sharing a DropBox folder with folks in my group. I am behind on programming exercises. Doing a lot of reading. I think I have all the programming for week 1 done. If you are interested in sharing my DropBox/PythonShare just email me at: richardgoodrich@gmail.com