简体   繁体   English

如何使用Python跟踪用户的猜测? 尝试次数=尝试次数+ 1不起作用

[英]How do I keep track of a user's guesses in Python? attempts= attempts + 1 is not working

I need to keep track of the number of guesses a user inputs in a simple guessing game. 我需要跟踪用户在简单的猜测游戏中输入的猜测数量。

I have tried using attempts= 0 and then setting attempts to = attempts + 1. Even when I do this, the code will print "You have guessed in 1 attempts" even when the user has guessed in more attempts than one. 我尝试使用trys = 0,然后将trys设置为= trys + 1。即使执行此操作,即使用户猜测的尝试次数超过一次,代码也会打印“您已经尝试了1次”。

Code: 码:

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)

You never update the attempts variable, you've created a new one called attempts_used , you don't need to do this. 您永远不会更新attempts变量,您已经创建了一个名为attempts_used的新变量,您不需要这样做。

Just use attempts everywhere you're using attempts_used 只在attempts使用的地方使用attempts_used

Note: Whilst you're at it you should get rid of what is known as a "magic number", or a hard coded limit in your while loop 注意:在执行此操作时,您应该摆脱while循环中所谓的“魔术数字”或硬编码限制

while attempts < tries_allowed:

Cleaned up your code a bit, shows the += counting method working for your script. 稍微整理一下代码,显示+=计数方法可用于您的脚本。 As others have said, the original code is creating an entirely new variable attempts_used that is simply attempts + 1 , and attempts remains 0. 就像其他人所说的那样,原始代码正在创建一个全新的变量attempts_used ,它只是attempts + 1 ,而attempts仍然为0。

It could also be attempts = attempts + 1 , += means the same thing. 也可能是attempts = attempts + 1+=表示同一件事。

To make a int a str in python for printing purposes, it does not need to be stored to a separate variable, just call str() around it, unless you plan to use the string separately. 为了使int一个str的Python为打印目的,它并不需要被存储到一个独立的变量,只需调用str()围绕它,除非你打算单独使用的字符串。

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)

您的“ attemps”变量保持为0,因此attemps_used(attempts + 1)始终为1。您必须将两个变量合并为一个变量才能对其进行控制(attempts = attempts + 1)

Code which also checks previous input and prints a message. 该代码还将检查先前的输入并显示一条消息。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM