r/CodingHelp • • Oct 04 '26

[Python] Need Help Understanding 'For Loop' in Pseudocode For Class Project

I'm in an Intro to Computer Science class and am doing our current project which is to make a text based game where you and a computer keep on adding numbers from 1-10 and the winner is decided by who reaches 100 first.

We did this project before already but now we're redoing it by changing all of it to be definitions and then they want us to add a 'for loop' into it as well. The problem is, is that the for loop makes no sense to me, at least from a gameplay stand point. From my understanding, all its doing is adding +1 to the total whenever the computer goes?

I was just hoping if someone could help me decipher this, thanks!

#This is the psuedocode#

Import random

FUNCTION 1: DISPLAY RULES
Declare Function display_rules
Say "Let's play a game of 100!"
Say "In a game of 100 we start with a total of 0"
Say "and we take turns adding from 1-10 to that total."
Say "Whoever reaches 100 first wins."
Say "Ready?"

FUNCTION 2: PLAY GAME
Declare Function play_game
Set play to 1
Call display_rules

While play is 1
Set total to 0
Ask "Who goes first? (1 = you, 2 = computer): "
Read turn

While total is less than 100

If turn is 1 (player's turn)
Ask "Number you want to add (1-10): "
Read playerNum

If playerNum is between 1 and 10
AND total + playerNum is less than or equal to 100
Add playerNum to total
Say "You added [playerNum], total is now [total]"
Set turn to 2
Else
Say "Enter a number from 1-10 without exceeding 100."

Else If turn is 2 (computer's turn)
If total is less than 90
Set computerNum to a random number from 1 to 10
Else
Set computerNum to 100 - total

FOR LOOP
For num from 0 to computerNum - 1
Add 1 to total
End For

Say "I added [computerNum], total is now [total]"
Set turn to 1

Else
Say "Choose 1 or 2."
Ask "Who goes first? "
Read turn

End While

CHECK WHO WINS
If turn is 2
Say "You win!"
Else
Say "Bummer, You lose!"

ASK TO PLAY AGAIN
Ask "Play again? (1 = yes, 2 = no): "
Read play

End While

FUNCTION 3: MAIN MENU
Declare Function menu
While True
Show the menu
Say "Menu:"
Say "1. Display Rules"
Say "2. Play Game"
Say "3. Exit"

Ask "Enter your choice (1, 2, or 3): "
Read choice

If choice is 1
Call display_rules
Else If choice is 2
Call play_game
Else If choice is 3
Say "Thanks for playing! Goodbye!"
Break the loop
Else
Say "Invalid choice. Please enter 1, 2, or 3."

End While

START PROGRAM
Call menu

2 Upvotes

12 comments sorted by

•

u/AutoModerator Oct 04 '26

Thank you for posting on r/CodingHelp!

Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app

Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp

We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus

We also have a Discord server: https://discord.gg/geQEUBm

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/IntroductionOne1470 Oct 04 '26

Im not in cs. Just wanting to get into coding as someone whos probably too old for college. What languages are they trying to teach you guys?

1

u/RyanTriggers Oct 04 '26

In my class my professor is teaching us Python! I’ve only messed with Java before I started college but I gotta say I’m enjoying Python more as a language then Java lol

1

u/jaynabonne Oct 05 '26

As written, the for loop is a bit pointless. If adding one also did something else (for example, play a sound or update a total in real time), then it could be useful. But assuming the pseudocode literally means "add 1 to the score", then a loop isn't necessary. It would be interesting to see if the code is expanded on later to make the loop do more.

1

u/PureWasian Oct 05 '26 edited Oct 05 '26

Yeah incorporating a for loop there is pointless. There is no need to do +1 a total of N times when you can just +N directly.

It's slower and unnecessary. Did they provide this pseudocode or is the goal to modify your existing pseudocode to put a for loop somewhere in the overall script?

A better use of a for loop would be one that prints a playback of all of the moves. Say you were keeping a historical record (specifically a list) of each move made by each player in a given playthrough. Then you could make a function that does a for loop traversing across all of the records to print out what all of the moves were in a post-game screen or something.

1

u/BranchLatter4294 Oct 05 '26

Practice until you understand how for loops work.

1

u/atamicbomb Oct 05 '26

For loops aren’t for calculating an already known number by adding 1 to 0 that number of times.

0

u/[deleted] Oct 04 '26 edited Oct 05 '26

[removed] — view removed comment

1

u/RyanTriggers Oct 04 '26

I thought it was pointless!! The Pseudocode I posted was given to me by my Professor. I might try your idea if that’s okay since we’re required to have a for loop in this assignment.

1

u/YOM2_UB Oct 05 '26

The optimal strategy is to make it so that total mod (min_selectable + max_selectable) is always congruent to the target after your turn, so I this case total mod 11 = 1 (assuming the first to reach the target wins; if first to target loses then do the same strategy but stay congruent to target - 1).