Lab 4 - Random list & f-strings

Directions for Labs


  • Submit your completed lab file online to CourSys (not Canvas). Labs are marked on completion, not correctness, so complete each part to the best of your ability and learn!
  • It is recommended that students attend in-person lab sections for lots of help!
    • If you would like to attend an in-person lab section, you are welcome to come to any (or more than one) lab section.
    • There is no need to attend the section you are enrolled in.
    • There is no need to attend any lab sections: there is no attendance taken.
    • You can complete the lab on your own time or own computer if you like.
  • While completing these labs, you are encouraged to help your classmates and receive as much help as you like. Assignments, however, are individual work and you must not work with another person on assignments.

Make List of Random Numbers


  1. Create a new file named lab4.py file.
    • You should use a good folder structure, such as creating a lab4 or labs folder, as mentioned in previous labs.
  2. Create a list of ten random numbers, each chosen between 1 and 100 inclusive.
    • Hint: You'll need to build a list. I suggest adding one element at a time using a loop. Create an empty list before your loop, such as my_list = []
    • Hint: You'll want a loop to run through 10 times.
    • Hint: my_num = random.randint(5, 50) gives a random number between 5 and 50 inclusive.
    • Hint: See lab 3 for more info on building a list.
    • It's OK if it randomly chooses the same number twice.
  3. Print the list to the screen to show it's OK.

Guess a Random Number (break)


  1. Ask the user 5 times to guess a number.
    • If the number they guessed is in the list, print "Correct ___ is in the list." (filling the guess into the blank.)
    • If the number they guessed is not in the list, print "Try again!".
    • Hint: Remember that input() gives you a string, not an integer.
  2. Add to the guessing loop above a feature that if the user just presses ENTER without entering any text that it leaves the loop.
    • Hint: You'll need to use the break statement to leave the loop. For example, this code will print the numbers 0-5:
      # Print 0-5
      for i in range(100):
          if i == 6:
              break  
          print(i)
    • Hint: When the user types in their guess, it's initially a string. Check if that input is the empty string ( x == ""). If it is the empty string then break out of the loop.
    • Hint: If it's not the empty string, then convert it to an integer and check if it's in the list.
  3. Test your program by guessing numbers in the list, numbers not in the list, and just pressing ENTER.

Two Lists and Print


  1. Create a new list which will hold only the odd randomly selected numbers.
    • Create a new empty list, and then loop through the previously selected random numbers. For each one that is odd, append that to the new list.
    • Hint: Setup a for loop to iterate through all the perviously randomly selected numbers.
    • Hint: Check each number if it's odd. If it is, append it to the other list.
    • Hint: Use modulo (%) to check the remainder.
    • Hint: Print the list of odd numbers so you can check it's correct.
  2. Print to the screen how many odd numbers you found using len().
  3. Go through all numbers between 1 and 100 inclusive and print to the screen:
    • Print "x is selected and odd!" if it's an odd number that was randomly selected.
    • Print "x is selected." if it not odd, but was randomly selected.
    • Print "x" if it was not randomly selected.
    • (Replace the number for "x")*
    • Each time you check if x is in one of the lists, you must use the Python in keyword.
    • Hint: When checking if it's selected as a random number, check if it's in your list of randomly selected numbers.
    • Hint: When checking if it's odd and selected, use the in keyword to check if it's in the odd randomly selected numbers.

Experiment with F-Strings


  1. Copy the following code to the end of your lab.

    name = "Dr. Evil"
    age = 42
    height = 1.28
    
    print(f"1. Name: {name:10}, Age: {age:5}, Height: {height:5f}m")
    print(f"2. Name: {name:10}, Age: {age:5}, Height: {height:5f}m")
    print(f"3. Name: {name:10}, Age: {age:5}, Height: {height:5f}m")
    print(f"4. Name: {name:10}, Age: {age:5}, Height: {height:5f}m")
    print(f"5. Name: {name:10}, Age: {age:5}, Height: {height:5f}m")
    print(f"6. Name: {name:10}, Age: {age:5}, Height: {height:5f}m")
    print(f"7. Name: {name:10}, Age: {age:5}, Height: {height:5f}m")
    print(f"8. Name: {name:10}, Age: {age:5}, Height: {height:5f}m")
    print(f"9. Name: {name:10}, Age: {age:5}, Height: {height:5f}m")    
  2. By changing only what appears in the {...} above, make the code output the following:
    1. Name: Dr. Evil  , Age:    42, Height: 1.280000m
    2. Name: Dr. Evil  , Age:    42, Height:   1.3m
    3. Name: Dr. Evil  , Age: 42   , Height: 1.3  m
    4. Name:   Dr. Evil, Age:    42, Height:   1.3m
    5. Name:  Dr. Evil , Age:  42  , Height:  1.3 m
    6. Name: Dr. Evil  , Age:    42, Height:  1.28m
    7. Name: Dr. Evil  , Age:    42, Height:     1m
    8. Name: Dr. Evil  , Age:    42, Height: 1.2800000000m
    9. Name: Dr. Evil            , Age:       42, Height:   1.3m 
    • First identify what changes for each line of the output. Line 1 is unchanged.
    • Recall that you can control:
      • How many columns (characters) are printed for each output.
      • Alignment of the output.
      • How many digits after the decimal point for floating point numbers.
    • Hint: Work through the output lines one at a time identifying what has changed and then change what's in the {...} as needed.
  3. Understanding Questions (answer as comments in your code)
    1. In the output for this section, even when left-aligned, why is there still a space between the "Name:" and the name?
    2. What's wrong with this code?
      pi = 3.14159
      print(f"Pi is {pi:5.4}")

Submission


Submit your lab4.py file to CourSys by Sunday midnight. Remember you can submit up to 3 days late with no penalty if needed (but please plan to get it done on time to stay up to date on the course!)

Topics Covered

  • for: loop over list; loop over i in range(...)
  • Working with lists:
    • len(..)
    • Working with multiple lists
    • list.append(...)
  • Formatted output with f-strings