Click here to Skip to main content
15,358,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am attempting to update the user score using return and for loops, the program is supposed to run 3 times and ask the user for the action they select, after every run the program should print the updated score. For instance, if a user selects hide their score is increased by 300 and it is printed, if they select hide again their score is once again increased. However, in this program, I keep getting the same number every time I run the program with the input "hide". Could someone accept this challenge and help me out?

Python
def userCalc():
    score=200

    for i in range(3):
        for userI in range(3):
            action= input("Enter (hide or run): ")
            
            def userF(fil):
                if fil=="hide":
                    update= score+200 
                    print (update)
                if fil=="run":
                    update= score+300 
                    print (update)
            userF(action)
                    
            
        return action
        
        return score
userCalc()


What I have tried:

I have tried creating a return statement but it did not work.
Posted
Updated 17-May-22 20:16pm
Comments
Patrice T 18-May-22 2:23am
   
Show code that we can run.

A couple of things:
1) You don't change score at any point, so it will remain at 200 for all time. Probably you want to add to it's value instead of creating a new variable called update.

2) return causes an immediate exit from the function, no further code is ever executed. So having any code (even another return after it is redundant because that later code will never be executed:
return action

return score


3) Both your return statements are inside the outer loop, so the outer loop will never execute more than once.

Basically, stop guessing and start thinking! This may help you to get started properly: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   
Comments
CPallini 18-May-22 2:15am
   
5.
Try
Python
def userCalc():
  score = 200

  def userF(fil, score):
    if fil=="hide":
      update= score+200
    elif fil=="run":
      update= score+300
    print (update)
    return update

  for i in range(3):
    action= input("Enter (hide or run): ")
    score = userF(action, score)
  return score

print("final score {}".format(userCalc()))
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900