-7

I don't know how to ask the user if they want to play again and how to make it if they say y it loops back to the start or n and the game exits. I know I could use a loop but I have no idea how to use them.

import random

xp = 0

level = 1

player1 = raw_input("player1 enter name")

enemyhealth = 100

playerhealth = 100

stage2 = "false"

difficulty = raw_input("choose difficulty, rookie,pro,master,legend or god")

if difficulty == "rookie":

    enemyhealth = enemyhealth - 15

if difficulty == "pro":

    enemyhealth = enemyhealth + 10

if difficulty == "master":

    enemyhealth = enemyhealth + 35

if difficulty == "legend":

    enemyhealth = enemyhealth + 40

if difficulty == "god":

    enemyhealth = enemyhealth + 60                                                            

print ("A quick tutorial: Make sure you type you actions with no spaces,  heres a list of the moves you can perform")

print ("Punch: A simple attack that has a short range of damage but is reliable.")

print ("flyingkick: not 100 percent reliable but if pulled off correctly can deal good damage.")

print ("uppercut: A somewhat reliable move that deals decent damage.")

print ("kick: A simple attack that has a short range of damage but is reliable.")

print ("stab: can be very powerful or deal next to no damage.")

print ("shoot: deadly if it hits, less so if you miss.")

print ("A man in the street starts a fight with you, beat him!")




while enemyhealth >0:

    fight = raw_input("fight")

    if fight == "punch":

        print ("You punched the enemy")

        enemyhealth = enemyhealth - random.randint(5,10)

        xp = xp + 1

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (0,5)

        print "you now have..", playerhealth

    elif fight =="flyingkick":

        print "you flying kicked the enemy"

        enemyhealth = enemyhealth - random.randint(5,25)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,15)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (0,5)

        print "you now have..", playerhealth

    elif fight =="uppercut":

        print "you uppercutted the enemy"

        enemyhealth = enemyhealth - random.randint(10,25)                                      

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,15)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="rko":

        print "you rko'ed the enemy, out of nowhere!"

        enemyhealth = enemyhealth - random.randint(9,29)                                      

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,12)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="kick":

        print "you kicked the enemy"

        enemyhealth = enemyhealth - random.randint(5,10)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="ninjastar":

        print "you ninjastarred the enemy"

        enemyhealth = enemyhealth - random.randint(5,20)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="headbutt":

        print "you headbutted the enemy"

        enemyhealth = enemyhealth - random.randint(5,18)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="deathray":

        print "you deathrayed the enemy"

        enemyhealth = enemyhealth - random.randint(1,50)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(1,30)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="stab":

        print "you stabbed the enemy"

        enemyhealth = enemyhealth - random.randint(3,40)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(2,20)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="shoot":

        print "you shot the enemy"

        enemyhealth = enemyhealth - random.randint(1,50)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(1,30)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    if xp == 5:

       print "you leveled up! move unlocked: 'rko'"

    if xp == 7:
       print "you leveled up! move unlocked: 'ninjastar'"

    if xp == 10:
       print "you leveled up! move unlocked: 'headbutt'"

    if xp == 100:
       print " you reached the max level! move 'deathray' is now unlocked"

    if playerhealth <1:

        print "you died!"

    if enemyhealth < 1:

        print "the enemy is KO , YOU WIN , Stage 2 is unlocked and the enemy has become stronger!"

        stage2 = "true"

    else:

        None
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 5
    *"please dont say wrap the code in a while loop because i have no idea what that means lol, thanks, and dont down vote please lol"* - good grief. Why don't you **learn about `while` loops**, then, and write like an adult? – jonrsharpe Feb 20 '16 at 11:12
  • See [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/4014959). – PM 2Ring Feb 20 '16 at 11:15
  • Please increase the line height in your editor so you don't have to write like this. – Emil Laine Feb 20 '16 at 11:30
  • @alexbwithtek and we should care about your deadlines because...? Please take the [tour] and learn [ask]. – jonrsharpe Feb 20 '16 at 11:35
  • @alex bwithtek I know your are so new programmer especial with python, So I tried to sparking.... – MLSC Feb 20 '16 at 12:11

1 Answers1

1

Waht I suggest you is: Read python documentation and functional programming. after that you can use this method in order to get you ideal results...

You can inspire from that:

#!/usr/bin/env python
import sys
import random

def play_game():
    print ' I am playing...' #Put your code here

def repeat(answer):
    if answer == 'y':
        print ' Good...'
        play_game()
    elif answer == 'n':
        print ' Exitting...'
        sys.exit()
    else:
        print ' Invalid choice!'
        return 'continue'

if __name__ == '__main__':
    try:
        play_game()
        while True:
            answer = raw_input(' Do you want to play again? [y/n]')
            ans = repeat(answer)
            if ans == 'continue':
                continue
    except KeyboardInterrupt:
        print '\n Ctrl+c signal recieved!'
        sys.exit()
MLSC
  • 5,872
  • 8
  • 55
  • 89
  • This asks you if you want to play again before you have played it the first time. – zondo Feb 20 '16 at 11:43
  • I would suggest that you define `repeat()` to take a prompt for an argument and have *it* ask the question. You could then put a `while` loop in your `repeat()` function that will ask `[y/n]` if the answer was not valid. It should keep asking until it gets a valid answer, and then return a boolean signifying the response. In your `try` block, then, you could say `while True:` `play_game()` `if not repeat("Do you want to play again? [y/n]"): sys.exit()`. But it's just a suggestion. – zondo Feb 20 '16 at 12:14
  • No problem... This is also possible I tried to just try this way... Thank you/// – MLSC Feb 20 '16 at 12:16