User Tools

Site Tools


basics_iii

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
basics_iii [2023/10/13 06:13] appledogbasics_iii [2023/10/13 11:48] (current) appledog
Line 58: Line 58:
 For homework ask them to include a round counter, which keeps track of how many guesses they make. For homework ask them to include a round counter, which keeps track of how many guesses they make.
  
 +== Advanced
 +Here is a "final" version which includes try and except error handling as well as easier logic.
 +
 +<Code:Python>
 +import random
 +
 +list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 +random.shuffle(list)
 +
 +score = 0
 +
 +while list != sorted(list):
 +    score = score + 1
 +    print(list)
 +    print("score:", score)
 +
 +    x = input("Switch two numbers (x): ")
 +    y = input("Switch two numbers (y): ")
 +
 +    try:
 +        x = int(x) - 1
 +        y = int(y) - 1
 +    except:
 +        print("")
 +        print("Oops! Please only enter numbers! :)")
 +        score = score - 1
 +        continue
 +
 +    (list[x], list[y]) = (list[y], list[x])
 +
 +    print ("==========")
 +
 +print(list)
 +print("you win!")
 +print("Your score is", score, "points.")
 +
 +</Code>
basics_iii.1697177628.txt.gz · Last modified: 2023/10/13 06:13 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki