Tôi cần theo dõi số lượng dự đoán mà một người dùng đầu vào trong một trò chơi đoán đơn giản.
Tôi đã thử sử dụng các nỗ lực = 0 và sau đó cài đặt các lần thử = cố gắng + 1. Ngay cả khi tôi thực hiện điều này, mã sẽ in "bạn đã đoán trong 1 lần thử" ngay cả khi người dùng đã đoán được nhiều lần hơn một.
Mã số:
attempts = 0;
print["Hello, welcome to the game. You will be choosing a number
between 1 and 100. You can only guess up to 10 times."]
for tries in range[tries_allowed]:
print["You only get 10 tries."]
break
while attempts < 10:
guess = int[input["Please guess a number"]];
attempts_used= attempts + 1;
if guess > random_number:
print["Guess is too high, try a smaller number"];
elif guess < random_number:
print["Guess is too low, try a higher number"];
elif guess == random_number:
attempts_used=str[attempts_used]
print["Correct- you win in", attempts_used, "guesses"];
exit[];
else:
if tries_allowed == 10:
print["You failed to guess in time"]
my_list= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list.append[attempts_used]
print[my_list]
Sayse
42K14 Huy hiệu vàng75 Huy hiệu bạc140 Huy hiệu đồng14 gold badges75 silver badges140 bronze badges
Đã hỏi ngày 9 tháng 5 năm 2019 lúc 16:19May 9, 2019 at 16:19
2
Bạn không bao giờ cập nhật biến attempts
, bạn đã tạo một biến số mới gọi là attempts_used
, bạn không cần phải làm điều này.
Chỉ cần sử dụng attempts
ở mọi nơi bạn đang sử dụng attempts_used
Lưu ý: Trong khi bạn ở đó, bạn nên loại bỏ cái được gọi là "số ma thuật" hoặc giới hạn mã hóa cứng trong vòng lặp trong khi của bạn
while attempts < tries_allowed:
Đã trả lời ngày 9 tháng 5 năm 2019 lúc 16:20May 9, 2019 at 16:20
NóiesayseSayse
42K14 Huy hiệu vàng75 Huy hiệu bạc140 Huy hiệu đồng14 gold badges75 silver badges140 bronze badges
2
Đã hỏi ngày 9 tháng 5 năm 2019 lúc 16:19
Bạn không bao giờ cập nhật biến attempts
, bạn đã tạo một biến số mới gọi là attempts_used
, bạn không cần phải làm điều này.
Chỉ cần sử dụng attempts
ở mọi nơi bạn đang sử dụng attempts_used
import random
random_number = random.randint[1,100]
attempts = 0
tries_allowed = 10
print["Hello, welcome to the game. You will be choosing a number between 1 and 100"]
print["You only get " + str[tries_allowed] + " tries."]
my_list = []
while attempts < tries_allowed:
guess = int[input["Please guess a number: "]]
if guess in my_list:
print["You have already guessed " + str[guess]]
continue
attempts += 1
my_list.append[guess]
if guess > random_number:
print["Guess is too high, try a smaller number"]
elif guess < random_number:
print["Guess is too low, try a higher number"]
elif guess == random_number:
print["Correct- you win in", str[attempts], "guesses"]
break
else:
if attempts == 10:
print["You failed to guess in time"]
for item in my_list:
print[item]
Lưu ý: Trong khi bạn ở đó, bạn nên loại bỏ cái được gọi là "số ma thuật" hoặc giới hạn mã hóa cứng trong vòng lặp trong khi của bạnMay 9, 2019 at 16:46
Đã trả lời ngày 9 tháng 5 năm 2019 lúc 16:20tgikal
Nóiesayse1 gold badge12 silver badges26 bronze badges
2
Làm sạch mã của bạn một chút, hiển thị phương thức đếm +=
hoạt động cho tập lệnh của bạn. Như những người khác đã nói, mã gốc đang tạo ra một biến hoàn toàn mới attempts_used
chỉ đơn giản là ____10 và attempts
vẫn còn 0.
Nó cũng có thể là
while attempts < tries_allowed:
2, +=
có nghĩa là điều tương tự.May 9, 2019 at 16:40
2
Để tạo một
while attempts < tries_allowed:
4 A while attempts < tries_allowed:
5 trong Python cho mục đích in, nó không cần phải được lưu trữ vào một biến riêng biệt, chỉ cần gọi while attempts < tries_allowed:
6 xung quanh nó, trừ khi bạn có kế hoạch sử dụng chuỗi riêng.import random
# Hello World program in Python
attempts = 0
print["Hello, welcome to the game. You will be choosing a number between 1 and 100. You can only guess up to 10 times."]
random_number = random.randint[1, 101]
#print[random_number]
my_list= []
for tries in range[10]:
print["You only get 10 tries."]
guess = int[input["Please guess a number: "]]
if guess in my_list:
print["You already guessed this number!!"]
my_list.append[guess]
attempts += 1
if guess > random_number:
print["Guess is too high, try a smaller number"]
elif guess < random_number:
print["Guess is too low, try a higher number"]
else:
print["Correct- you win in", tries + 1, "guesses"]
attempts = -1
break
if attempts is 10 and attempts is not -1:
print["You failed to guess in time"]
print["Attempts : ", my_list]
Đã trả lời ngày 9 tháng 5 năm 2019 lúc 16:46May 9, 2019 at 17:29
TgikaltgikalAnakin
1.6221 Huy hiệu vàng12 Huy hiệu bạc26 Huy hiệu đồng1 gold badge11 silver badges25 bronze badges