8000 session 5 · jtang1/intro-to-python@38100cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 38100cf

Browse files
author
Replit user
committed
session 5
1 parent 3201d29 commit 38100cf

File tree

1 file changed

+154
-1
lines changed

1 file changed

+154
-1
lines changed

session_05/exercises_5.py

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,122 @@
33
## Section A
44
# 1. Print 10 random numbers.
55

6+
# import random
7+
# x = 0
8+
# while x < 9:
9+
# print(random.randint(0, 100))
10+
# x += 1
611

712

813
# 2. Keep asking the user to enter a number until they enter the number 7, then print "Wow lucky number 7!".
914
# - Rewrite so that the number being guessed is a random value from 1 to 10 instead of number 7 .
1015

16+
# number = None
1117

18+
# while number != 7:
19+
# number = int(input("Enter a number: "))
20+
21+
# print("Wow, lucky number 7!")
1222

1323
# 3. The area of a rectangle is width multiplied by height. Ask the user to enter a width and height in cm, then print the area to the whole square metre.
1424
# E.g. 240cm x 80cm = 19200cm2 = 2m2.
25+
# import math
26+
# width = int(input("Enter a width in cm: "))
27+
# length = int(input("Enter a height in cm: "))
28+
# area_cm = width * length
29+
# area_m = math.floor(area_cm/10000)
1530

31+
# print("The area is " + str(area_m) + "m^2")
1632

1733

1834
# 4. Ask the user for a password, if they enter the password "qwerty123", print "You have successfully logged in".
1935
# If they get it wrong, print "Password failure" and then ask them to enter it again.
2036
# Only allow them to enter the password wrong 3 times before printing "System Locked!".
2137

22-
38+
# password = None
39+
# attempts = 0
40+
41+
# while attempts < 3:
42+
# if password != "qwerty123":
43+
# password = input("Please enter your password: ")
44+
# print("Password failure")
45+
# attempts += 1
46+
# else:
47+
# print("You have successfully logged in")
48+
# break
49+
50+
51+
# if attempts == 3:
52+
# print("You have entered your password incorrectly 3 times.")
53+
# print("System Locked!")
54+
55+
56+
# password = input("Please enter your password: ")
57+
# correct_password = "qwerty123"
58+
# attempts = 0
59+
60+
# while attempts <2:
61+
# if password == correct_password:
62+
# print("You have successfully logged in.")
63+
# break
64+
# else:
65+
# print("Password failure")
66+
# attempts += 1
67+
# password = input("Please enter your password: ")
68+
69+
# if attempts == 2:
70+
# print("System failure - you have entered your password incorrectly 3 times.")
71+
2372

2473
# 5. Add up all the numbers from 1 to 500 and print the answer.
2574

75+
# number = 0
76+
# sum = 0
77+
78+
# while number < 501:
79+
# sum = sum + number
80+
# number += 1
81+
82+
# print(sum)
2683

2784

2885
# 6. Create a blank list. Ask the user to input 10 numbers (one should be the number 99) into the list.
2986
# Find the index at which the user entered the number 99.
3087

88+
# list = []
3189

90+
# index = 0
3291

92+
# while len(list) < 10:
93+
# user_input = list.append(int(input("Enter 10 numbers, one of which should be 99: ")))
94+
95+
# for each_number in list:
96+
# if each_number != 99:
97+
# index += 1
98+
# else:
99+
# print("99 is at index number " + str(index))
100+
33101
# 7. Print a multiplication table for the number 18 up to 15. E.g.
34102
# 1 x 18 = 18
35103
# 2 x 18 = 36
36104

37105

106+
# for each_number in range(1, 16):
107+
# print(str(each_number) + " x 18 = " + str(each_number*18))
108+
109+
# i = 0
110+
111+
# while i < 15:
112+
# print(str(i) + " x 18 = " + str(i * 18))
113+
# i += 1
38114

39115
# 8. Print the numbers 1 to 100 (including the number 100) using a while loop.
40116

117+
# i = 0
41118

119+
# while i < 101:
120+
# print(i)
121+
# i += 1
42122

43123
# 9. Rewrite question B8 from session 3 using a while loop
44124
# - A school has following rules for their grading system:
@@ -50,13 +130,86 @@
50130
# f. Below 25 - F
51131
# Ask user to enter the lesson and the marks for three lessons and print out the corresponding grades for the lesson.
52132

133+
# lesson = None
134+
135+
# print("REPORT CARD:")
136+
137+
# while lesson != "":
138+
# lesson = input("Input your lesson:\n")
139+
# mark = int(input("Input your mark:\n"))
140+
# if mark > 80:
141+
# print(lesson + " - A grade")
142+
# elif mark <= 80 and mark > 60:
143+
# print(lesson + " - B grade")
144+
# elif mark <= 60 and mark > 50:
145+
# print(lesson + " - C grade")
146+
# elif mark <= 50 and mark> 45:
147+
# print(lesson + " - D grade")
148+
# elif mark <= 45 and mark > 25:
149+
# print(lesson + " - E grade")
150+
# elif mark < 25:
151+
# print(lesson + " - F grade")
152+
# else:
153+
# print("Go to see your teacher")
53154

54155

55156
# 10. Ask the user to enter the names of people who entered a prize draw. Select a random winner and print their name
56157

158+
# import random
159+
160+
# list_of_names = []
161+
162+
# user_input = None
163+
164+
# while user_input != "":
165+
# user_input = input("Enter the name of someone who entered a prize draw: ")
166+
# list_of_names.append(user_input)
167+
168+
# # print(list_of_names)
169+
# # print(len(list_of_names))
170+
# del(list_of_names[len(list_of_names) - 1])
171+
# # print(list_of_names)
172+
# print("The winner is " + random.choice(list_of_names))
173+
57174

58175

59176
# 11. Create a rock, paper, scissors game which is run against computer.
60177
# This is a game where you select either rock/paper/scissors and you compare it to your opponents choice.
61178
# Rock beats scissors, paper beats rock, scissors beats paper. If both choose the same, then you play again.
62179
# + Expand this so that its best of 3 games
180+
181+
import random
182+
183+
user_wins = 0
184+
computer_wins = 0
185+
options = ["Rock", "Paper", "Scissors"]
186+
187+
while user_wins < 2 and computer_wins < 2:
188+
user_input = input("Choose from Rock, Paper, or Scissors: ")
189+
computer_input = random.choice(options)
190+
print("Computer chose " + computer_input)
191+
if user_input == "Rock" and computer_input == "Paper":
192+
print("Computer wins!")
193+
computer_wins += 1
194+
elif user_input == "Rock" and computer_input == "Scissors":
195+
print("User wins!")
196+
user_wins += 1
197+
elif user_input == "Paper" and computer_input == "Scissors":
198+
print("Computer wins!")
199+
computer_wins += 1
200+
elif user_input == "Paper" and computer_input == "Rock":
201+
print("User wins!")
202+
user_wins += 1
203+
elif user_input == "Scissors" and computer_input == "Rock":
204+
print("Computer wins!")
205+
computer_wins += 1
206+
elif user_input == "Scissors" and computer_input == "Paper":
207+
print("User wins!")
208+
user_wins += 1
209+
else:
210+
print("It's a draw! Play again")
211+
print("User wins: " + str(user_wins))
212+
print("Computer wins: " + str(computer_wins))
213+
214+
215+

0 commit comments

Comments
 (0)
0