This is just a simple counter, i was able to code it in two different ways but i want to know which way is more efficient and which way is more readable. and which set of code is more "correct" in broad-manner of speaking. and if there is another entirely different set of code that does the same function ?
Game Rules
This is a simple game , at first it asks the user if they want to go right or left . if the user picks left ; the program ends and it displays the "You Win!" message. however if the user picks anything else other than left ( for example : right ) the program will start a counter 'z' and it will display a different message for each time the user chooses the wrong direction or input. i have set it to 4 different messages and then one last message that keeps playing on repeat until the user picks the correct direction.
however i feel like that there must be better code to do this. can you please help?
the first set of code :
#This is a simple game where if you pick left you win,
#but if you pick right you keep going through 4 different levels of
#'you lose' messages and then a last one keeps repeating in a loop.
z = 0
n = input ( "Go left or right? \n" )
while n not in ["left", "Left"]:
if z == 0 :
print ("You lose * 0 ")
z = z+1
n = input ( "Go left or right? \n" )
elif z == 1 :
print ("You lose * 1 ")
z = z+1
n = input ( "Go left or right? \n" )
elif z == 2 :
print ("You lose * 2 ")
z = z+1
n = input ( "Go left or right? \n" )
elif z == 3 :
print ("You lose * 3 ")
z = " "
n = input ( "Go left or right? \n" )
else :
while n not in ["left", "Left" ] :
print ( "You Lose * Infinite" )
n = input ( "Go left or right? \n" )
print ( "\nYou Win!" )
Second set of code :
#This is a simple game where if you pick left you win,
#but if you pick right you keep going through 4 diffrent levels of
#'you lose' messegses and then a last one keeps repeating on a loop.
v = 0
z = 0
u = 0
x = 0
n = 0
while x == 0 :
n = input ("Go Left or Right?")
while n not in ["left", "Left"] and u == 0 :
if z == 0:
print ( " You lose * 0 ")
elif z == 1:
print ( " You lose * 1 ")
elif z == 2:
print ( " You lose * 2 ")
elif z == 3:
print ( " You lose * 3 ")
else :
print ( " You lose * Infinite " )
u = " "
while z < 4 and v == 0 :
z = z + 1
v = " "
u = 0
v = 0
if n == "left" or n == "Left":
x=" "
print ( " You Win! ")
final set of code :
user = input ("Go left or right?")
counter = 0
while user not in ["left", "Left"] :
if counter == 0 :
print ("You lose * 0")
elif counter == 1 :
print ("You lose * 1")
elif counter == 2 :
print ("You lose * 2")
elif counter == 3 :
print ("You lose * 3")
else :
print ("You lose * Infinite")
counter = counter + 1
user = input ("Go left or right?")
print ("You Win!")