Hướng dẫn how do you code the number of attempts in python? - làm cách nào để mã số lần thử trong python?

Tôi đang cố gắng tạo một trò chơi số ngẫu nhiên trong Python, nơi máy tính phải tạo ra một số từ 1 đến 20 và bạn phải đoán nó. Tôi đã giới hạn số lượng dự đoán ở 6. Làm cách nào để in bao nhiêu người mà người dùng đã để lại khi họ đoán sai? Đây là mã của tôi:

import random

attempts = 0

name = input["What is your name? "]
random = random.randint[1, 20]
print[name + ",","I'm thinking of a number between 1 and 20, What is it?"]

while attempts < 6:
    number = int[input["Type your guess: "]]
    attempts = attempts + 1
    int[print[attempts,"attemps left"]] #This is the code to tell the user how many attempts left
    if number < random:
        print["Too low. Try something higher"]
    if number > random:
        print["Too high. Try something lower"]
    if number == random:
        break
if number == random:
    if attempts = 4:
        print["Well done,",name + "! It took you",attempts,"attempts. Athough, next time try to get three attempts or lower"]
if number != random:
    print["Sorry. All your attempts have been used up. The number I was thinking of was",random]

Cảm ơn, bất kỳ sự giúp đỡ được đánh giá rất cao!

< Back to Problems

Viết một chương trình có người dùng đoán tên của bạn, nhưng họ chỉ có 3 cơ hội để làm như vậy cho đến khi chương trình thoát.

name = raw_input["What's my name? "]
answer = "Jack"
attempt = 0

if name == answer:
	print["That's correct!"]
else:
	while name != answer and attempt a:
            print["you need to guess lower. Try again"]
            guess = int[input["Guess the hidden number"]]
        if guess == a:
            print['it took you:',i,'tries to guess the hidden number']
            print['Congratulations, the hidden number is',a]
    repeat = input["\nDo you want to try again Y/N?  \n>>> "]
        
while repeat.lower[] == 'n': 
    print['thank you']
    break

Cập nhật 11-Dec-21 19:52 PM 11-Dec-21 19:52pm

Thêm một biến bên ngoài vòng lặp và khởi tạo nó thành 0. bên trong vòng lặp, kiểm tra số lượng. Nếu nó lớn hơn giới hạn, hãy nói với họ rằng họ đã thất bại và thoát khỏi vòng lặp. Nếu không, thêm một vào nó, và để họ thử lại.
Inside the loop, check the count.
If it's bigger than the limit, tell them they failed, and exit the loop.
Otherwise, add one to it, and let them try again.

Nội dung này, cùng với bất kỳ mã nguồn và tệp liên quan nào, được cấp phép theo giấy phép mở của Dự án Mã [CPOL]



Codeproject, 20 Bay Street, Tầng 11 Toronto, Ontario, Canada M5J 2N8 +1 [416] 849-8900

Làm cách nào để thực hiện 3 lần thử trong Python?

Để lấy tên người dùng và giá trị nhập mật khẩu với 3 lần thử: sử dụng vòng lặp thời gian để lặp lại tối đa 3 lần.Sử dụng hàm input [] để lấy các giá trị cho tên người dùng và mật khẩu từ người dùng.Nếu thông tin đăng nhập là chính xác, thoát ra khỏi vòng lặp.Use a while loop to iterate a maximum of 3 times. Use the input[] function to take values for the username and password from the user. If the credentials are correct, break out of the loop.

Làm thế nào để bạn cố gắng trong Python?

Trả lời và cố gắng

Bài Viết Liên Quan

Chủ Đề