0

I am trying to get a list of scores from user input. I want each prompt to include the current game number, which will increment from 1 to the total number of games. For example, if the user wants to input 3 games, the prompts should look like this:

score for game 1
score for game 2
score for game 3

Here's my code without the incrementing:

n = int(input("enter total number of games "))
games = []
for i in range(n):
    x = int(input("score for game 1 "))
    games.append(x)

How can I modify this so that the "1" increments from 1 to n?

1
  • This question is similar to: How can I increment the numerical part of a string?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 19, 2024 at 15:23

2 Answers 2

2

Don't try to manually increment anything - since you're using a for loop over a range, you can use the value of the iteration variable (i) as your "incrementing" variable. You'll have to adjust the bounds from [0, n-1] to [1, n], which you can do in your for statement. Then you can use an f-string for the actual printing; it's the most concise and (to me) most readable way to use an int variable in a string.

n = int(input("enter total number of games "))
games = []
for i in range(1, n+1):
    x = int(input(f"score for game {i} "))
    games.append(x) 
Sign up to request clarification or add additional context in comments.

Comments

1

Just replace your highlighted line of code with the following code:

x = int(input(f"Score for game {i + 1}: "))

This would dynamically display the game number in the prompt rather than hardcoding it to "game 1".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.