Home > Mobile >  how do you use loops? and when?
how do you use loops? and when?

Time:03-17

Im struggling to use while so i want to ask here for clarity and help on use of it. Because i am quite new to coding so i dont fully understand when to use them. So it could turn out that i need to use something else but i just need something to loop or return to the start of the wolf.


print(r"""        _
       / \      _-'
     _/|  \-''- _ /
__-' { |          \
    /             \
    /       "o.  |o }
    |            \ ;
                  ',
       \_         __\
         ''-_    \.//
           / '-____'
          /
        _'
      _-'""")


w=int(input("woof do you want to play a game?\n1)Yes\n2)No\n"))

if w > 2:
  print("Invalid character")
  input("woof do you want to play a game?\n1)Yes\n2)No\n")

if w == 1:
  print("Then lets play heads or tails")
else:
  quit
ans=int(input("What do you choose?\n1)Heads\n2)Tails\n"))


if ans > 2:
  print("Invalid character")
  input("What do you choose?\n1)Heads\n2)Tails\n")

if ans == 1:
  print("Oh so you have chosen Heads")
if ans == 2:
  print("Oh so you have chosen Tails")

coin=random.randint(1,2)


if ans == coin:
  print(r"""                     .
                    / V\
                  / `  /
                 <<   |
                 /    |
               /      |
             /        |
           /    \  \ /
          (      ) | |
  ________|   _/_  | |
<__________\______)\__)      
Oh no you have Won""")


if ans != coin:
  print("I have won this game")
  
red=int(input("Would you like to play again?\n1)Yes\n2)No\n"))

CodePudding user response:

This is what I would recommend. You could put the entire thing in the while loop, but even then you would at least want your ascii art to be outside of it for tabbing purposes.

def wolf():
    global begin_wolf, win_wolf
    print(begin_wolf)


    w=int(input("woof do you want to play a game?\n1)Yes\n2)No\n"))

    if w > 2:
        print("Invalid character")
        input("woof do you want to play a game?\n1)Yes\n2)No\n")

    if w == 1:
        print("Then lets play heads or tails")
    else:
        quit
    ans=int(input("What do you choose?\n1)Heads\n2)Tails\n"))


    if ans > 2:
        print("Invalid character")
        input("What do you choose?\n1)Heads\n2)Tails\n")

    if ans == 1:
        print("Oh so you have chosen Heads")
    if ans == 2:
        print("Oh so you have chosen Tails")

    coin=random.randint(1,2)


    if ans == coin:
        print(win_wolf)


    if ans != coin:
        print("I have won this game")
  

begin_wolf = r"""        _
       / \      _-'
     _/|  \-''- _ /
__-' { |          \
    /             \
    /       "o.  |o }
    |            \ ;
                  ',
       \_         __\
         ''-_    \.//
           / '-____'
          /
        _'
      _-'"""
win_wolf = r"""                     .
                    / V\
                  / `  /
                 <<   |
                 /    |
               /      |
             /        |
           /    \  \ /
          (      ) | |
  ________|   _/_  | |
<__________\______)\__)      
Oh no you have Won"""

red = 1
while red == 1:
    wolf()
    red = int(input("Would you like to play again?\n1)Yes\n2)No\n"))
  • Related