Python Forum
Beginner problem, replace function with for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner problem, replace function with for loop
#1
I am new to programming with python so be gentle Blush

I'm going through the Introduction to Python on If Statements and working on a general challenge:

"Alien Points
Make a list of ten aliens, each of which is one color: 'red', 'green', or 'blue'.
You can shorten this to 'r', 'g', and 'b' if you want, but if you choose this option you have to include a comment explaining what r, g, and b stand for.
Red aliens are worth 5 points, green aliens are worth 10 points, and blue aliens are worth 20 points.
Use a for loop to determine the number of points a player would earn for destroying all of the aliens in your list."

It included this hint too:
"After you define your list of aliens, set a variable called current_score or current_points equal to 0.
Inside your for loop, write a series of if tests to determine how many points to add to the current score.
To keep a running total, use the syntax current_score = current_score + points, where points is the number of points for the current alien."


Despite the hint, I'm rather lost how to continue even with the hint. Instead, the best I could do was this using a function for each color aliens and their sum (below). Can someone point me in the right direction? Using for loops and if statements to produce this kind of result is what I'm lost on. Thanks for reading.

aliens = ['gobblegorp','andremodes','palpazar','schleem','thonbar', 'arachnio','urfur', 'vertos', 'auther','holik']


red_aliens = aliens[0:4]
green_aliens = aliens[4:7]
blue_aliens = aliens[7:-1]

red_score = (len(red_aliens) * 5)
green_score = (len(green_aliens) * 10)
blue_score = (len(blue_aliens) * 20)

print('The red aliens worth 5 pts each:')
for reddies in red_aliens:
    print(reddies.title())
print('\nThe green aliens worth 10 pts each: ')
for greens in green_aliens:
    print(greens.title())
print('\nThe blue aliens worth 20 pts each:')
for blues in blue_aliens:
    print(blues.title())

def alien_scorer(score):
    score = red_score + green_score + blue_score
    print("\nHere is the score from the reds: ")
    print(red_score)
    print("\n...from the reds and greens: ")
    print(red_score + green_score)
    print("\n...And from all red, green, and blues : ")
    print(score)

alien_scorer(aliens)
Reply


Messages In This Thread
Beginner problem, replace function with for loop - by Motley_Cow - Sep-11-2019, 01:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem program runs in a loop jasserin 0 209 May-18-2024, 03:07 PM
Last Post: jasserin
  While Loop Problem Benno2805 1 642 Sep-06-2023, 04:51 PM
Last Post: deanhystad
  Replace for loop to search index position illmattic 5 1,435 Sep-03-2022, 04:04 PM
Last Post: illmattic
  Loop reading csv file problem faustineaiden 1 1,643 Dec-11-2021, 08:40 AM
Last Post: ibreeden
  small beginner problem FelixReiter 2 1,953 Nov-17-2020, 03:26 PM
Last Post: FelixReiter
  Infinite loop problem Zirconyl 5 3,164 Nov-16-2020, 09:06 AM
Last Post: DeaD_EyE
  Exit Function - loop Tetsuo30 2 2,155 Sep-17-2020, 09:58 AM
Last Post: Tetsuo30
  Beginner having Syntax Error problem RyanHo 3 2,498 Sep-10-2020, 08:33 AM
Last Post: cnull
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,248 Aug-28-2020, 02:41 PM
Last Post: sparkt
  basic random number generator in replace function krug123 2 2,130 Jul-31-2020, 01:02 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020