-3

I'm working on my first project, but I've encountered a problem, I am trying to make a program that randomly generates a skateboarding trick for you to do, and if you can do the trick, then you get +1 points and if not then 0 points. everything else runs smoothly, but the points are not stacking up. Here is the code.

import random
import time

print()
print("Welcome to 'Can you really skate?'")
print()
                                      # starting by defining the different levels
print("Level")
print("1: Beginner")
print("2: Intermidiete")
print("3: Kind of advanced")
print("4: Advanced")

level = input("Choose your level: ")  # asking the user to input which level they are at
points = int(0)

def main():
    if level == "1":
        stance = ["fakie", "switch", "regular", "nollie"]
        trick = ["ollie", "shuv-it", "frontside 180", "backside 180"]
        print("Your random trick is:", random.choice(stance), " ", random.choice(trick))

    elif level == "2":
        stance = ["fakie", "switch", "regular", "nollie"]
        trick = ["ollie", "shuv-it", "frontside 180", "backside 180", "kickflip", "heelflip", "popshuv-it", "frontside popshuv-it", "bigspin", "varialkickflip"]
        print("Your random trick is:", random.choice(stance), " ", random.choice(trick))

    time.sleep(4)

    # so here is where the problem is, when the user types in "yes", it
    # should add points which should then be stored in the variable and
    # then added on when it loops again

    print("did you make your trick? ")

    make = input("yes / no ?")

    if make == "yes":
        points = 1

    elif make == "no":
        points = int(0)

    print(f"You have {points} points")

    play = input("play again? yes/no ?")

    if play =="yes":
        main()
    elif play =="no":
        print("you have compleated the game, your final score is: ", points)
        exit()

main()

The goal is to make the program add on points for each time the user types "yes" to the question, and then the points to be displayed at the very end when the user types "no" to the question "play again?"

martineau
  • 119,623
  • 25
  • 170
  • 301
  • FWIW, `int(0)` is redundant. Just `0` will do just fine… – deceze Aug 31 '21 at 15:30
  • 1
    Do you mean `points += 1` instead of `points = int(+ 1)`? You don't need to use `int` unless you're casting, and there's no need to do recursion here. Use a loop, otherwise if you do enough tricks, the program crashes for no reason. For future reference, try to _minimize_ the problem. Strip out all of the unnecesssary stuff until you're left with `number = 40; number = int(+ 1); print(number);` and it'll be much more obvious what the problem is and how to search for an answer "increment number in python" or similar. – ggorlen Aug 31 '21 at 15:30
  • `points += 1` works fine – Olvin Roght Aug 31 '21 at 15:30
  • Use `points += 1`, this is short hand for `points = points + 1`. You need to overwrite the points variable with a new integer which has an addition of 1. – ChrisOram Aug 31 '21 at 15:30

3 Answers3

0

I'll reiterate what the comments have said about you needing to use points += 1. However, there is another issue. When the user types yes you actually call main() again. This isn't a good way to continue running the script; instead you should use a loop like so:

import time

print()
print("Welcome to 'Can you really skate?'")
print()
                                                        #starting by defining the different levels
print("Level")                                                        
print("1: Beginner")
print("2: Intermidiete")
print("3: Kind of advanced")
print("4: Advanced")

level = input("Choose your level: ")    #asking the user to input which level they are at
points = int(0)

def choose_trick():
    if level == "1":
        stance = ["fakie", "switch", "regular", "nollie"]
        trick = ["ollie", "shuv-it", "frontside 180", "backside 180"]
        print("Your random trick is:", random.choice(stance), " ", random.choice(trick))

    elif level == "2":
        stance = ["fakie", "switch", "regular", "nollie"]
        trick = ["ollie", "shuv-it", "frontside 180", "backside 180", "kickflip", "heelflip", "popshuv-it", "frontside popshuv-it", "bigspin", "varialkickflip"]
        print("Your random trick is:", random.choice(stance), " ", random.choice(trick))

    time.sleep(4)

#so here is where the problem is, when the user types in "yes", it 
#should add points which should then be stored in the variable and 
#then added on when it loops again

    print("did you make your trick? ")

    make = input("yes / no ?")

    global points
    if make == "yes":
        points += 1
    #The else case has no purpose if points is not be modified                      

    print(f"You have {points} points")

    play = input("play again? yes/no ?")

    if play =="yes":
        return True
    elif play =="no":
        print("you have compleated the game, your final score is: ", points)
        exit()

def main():
    while choose_trick():
        pass

if __name__ == '__main__':
    main() 
BTables
  • 4,413
  • 2
  • 11
  • 30
0

what you are doing is you are assigning 1 to points variable (points = 1), you are not actually incrementing it, which will look like(points = points +1 or points +=1), since you are declaring your points variable outside function you have call it has global points, and resetting the points variable in if play == no condition

sample code

import time
import random

print()
print("Welcome to 'Can you really skate?'")
print()
                                                        #starting by defining the different levels
print("Level")
print("1: Beginner")
print("2: Intermidiete")
print("3: Kind of advanced")
print("4: Advanced")

level = input("Choose your level: ")    #asking the user to input which level they are at
points = 0

def main():

    if level == "1":
        stance = ["fakie", "switch", "regular", "nollie"]
        trick = ["ollie", "shuv-it", "frontside 180", "backside 180"]
        print("Your random trick is:", random.choice(stance), " ", random.choice(trick))

    elif level == "2":
        stance = ["fakie", "switch", "regular", "nollie"]
        trick = ["ollie", "shuv-it", "frontside 180", "backside 180", "kickflip", "heelflip", "popshuv-it", "frontside popshuv-it", "bigspin", "varialkickflip"]
        print("Your random trick is:", random.choice(stance), " ", random.choice(trick))

    time.sleep(4)

#so here is where the problem is, when the user types in "yes", it
#should add points which should then be stored in the variable and
#then added on when it loops again

    print("did you make your trick? ")

    make = input("yes / no ?")

    global points
    if make == "yes":
        points += 1

    elif make == "no":
        points = int(0)

    print(f"You have {points} points")

    play = input("play again? yes/no ?")

    if play =="yes":
        main()
    elif play =="no":
        print("you have compleated the game, your final score is: ", points)
        points = 0
        exit()


main()
   
0

There are a couple of related problems with your code that are causing the problem with the summing of the points value. One is that it is a local variable in the main() function, which means that each time main calls itself, a new version of the variable is created. This is not a good way to loop in this situation because using an explicit loop like a while statement would allow the value of the variable be retained without resorting to using global variables which are usually bad news.

The second issue is with the way you're trying to incrementing the variable. This should be done with either points = points + 1 or the more succinct points += 1, and you should leave out the elif part with points = int(0) because that will reset the total back to zero.

Below is a revised version of your code. Besides fixing the above two problems, I've changed all the places where input from the user is gathered so that one of the techniques shown in the accepted answer to the question Asking the user for input until they give a valid response is used instead.

Here's the revised version:

import random
import time

print()
print('Welcome to "Can you really skate?"')
print()

# Menu of the different levels.
print("Level")
print("1: Beginner")
print("2: Intermidiete")
print("3: Kind of advanced")
print("4: Advanced")
print()

while True:  # Ask user to input which level they are at
    try:
        level = int(input("Choose your level: "))
    except ValueError:
        print("Please enter a number from 1 to 4.")
        continue

    if level in range(1, 5):
        break  # Leave loop, got a valid value.
    else:
        print("Sorry, the level must be from 1 to 4.")


def main():
    points = 0  # Initialize local variable.

    while True:
        if level == 1:
            stance = ["fakie", "switch", "regular", "nollie"]
            trick = ["ollie", "shuv-it", "frontside 180", "backside 180"]
        elif level == 2:
            stance = ["fakie", "switch", "regular", "nollie"]
            trick = ["ollie", "shuv-it", "frontside 180", "backside 180", "kickflip",
                     "heelflip", "popshuv-it", "frontside popshuv-it", "bigspin",
                     "varialkickflip"]
        else:
            raise NotImplementedError('Unfinished code')

        print(f"Your random trick is: {random.choice(stance)} {random.choice(trick)}")
        time.sleep(4)

        print("Did you make your trick?")
        while True:
            make = input("yes or no? ").lower()
            if make not in {"yes", "no"}:
                print('Please enter "yes" or "no".')
                continue
            if make == "yes":
                points += 1  # Increment value of local variable.
                print(f"You now have {points} points.")
            break

        print("Play again?")
        while True:
            play_again = input("yes or no? ").lower()
            if play_again not in {"yes", "no"}:
                print('Please enter "yes" or "no".')
                continue
            break

        if play_again == "no":
            break

    print(f"You have completed the game with a final score of {points}")


main()

martineau
  • 119,623
  • 25
  • 170
  • 301