My Tutor has set us a task: Create a higher/lower number guessing game with random integers. We must ask the user for a maximum value to generate and how many values we want to play.
For example:
max value: 12
number of values: 6
3 Hi(H) Lo(L):H
7 Hi(H) Lo(L):L
9 Hi(H) Lo(L):L
12 Hi(H) Lo(L):L
10 Hi(H) Lo(L):L
4
Your score is:4
The code I tried:
import random
print("This program plays out HI/LO game with random integer values")
mx_val = int(input("Enter maximun value: "))
num_val = int(input("Enter how many values to play(less than max value): "))
print("Get ready to play the game")
a = []
while len(a) != num_val:
r = random.randint(1, mx_val)
if r not in a:
a.append(r)
score = 0
for i in a:
print(a[0], end=" ")
guess = input("Enter Hi(H)or Lo(L): ")
while guess != "H" and guess != "L":
guess = input("You must enter 'H' or 'L': ")
if guess == "H" and a[1] > a[0]:
score += 1
if guess == "L" and a[1] < a[0]:
score += 1
a.pop(0)
print("Final score is ", score)
This is my code but it does not ask the right amount of questions. It's always way short.
a
list. You need to increase it by oneCode:
import random
print("This program plays out HI/LO game with random integer values")
mx_val = int(input("Enter maximun value: "))
num_val = int(input("Enter how many values to play(less than max value): "))
print("Get ready to play the game")
a = []
while len(a) != num_val+1:
r = random.randint(1, mx_val)
if r not in a:
a.append(r)
score = 0
for i in range(num_val):
print(a[0], end=" ")
guess = input("Enter Hi(H)or Lo(L): ")
while guess != "H" and guess != "L":
guess = input("You must enter 'H' or 'L': ")
if guess == "H" and a[1] > a[0]:
score += 1
if guess == "L" and a[1] < a[0]:
score += 1
a.pop(0)
print("Final score is ", score)
Output:
This program plays out HI/LO game with random integer values
Enter maximun value: 12
Enter how many values to play(less than max value): 6
Get ready to play the game
10 Enter Hi(H)or Lo(L): H
4 Enter Hi(H)or Lo(L): L
8 Enter Hi(H)or Lo(L): H
7 Enter Hi(H)or Lo(L): L
9 Enter Hi(H)or Lo(L): H
11 Enter Hi(H)or Lo(L): L
Final score is 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.