简体   繁体   English

为什么这段代码不起作用(我是编程新手,python)

[英]Why is this code not working (i'm new to programming, python)

import random

guesses = 3
number = random.randint(1, 10)
print (number) #<- To test what number im supposed to write.

while guesses > 0: # When you have more than 0 guesses -> guess another number
    guess = input("Guess: ")

    if guess == number : # If guess is equal to number -> end game
        print ('Your guess is right!.')
        break


    if guess != number : # If not number -> -1 guess
        print ("Nope!")
        guesses -= 1

    if guesses == 0: #If all 3 guesses are made -> end game
        print("That was it! Sorry.")
        print(number,"was the right answer!")

What am i doing wrong? 我究竟做错了什么? I can't figure it out, i hope you can help ^-^ 我想不通,希望你能帮忙^ - ^

And if you are able to teach me how to improve my programming then feel free to write me how to do it! 如果你能教我如何改进我的编程,那么请随时写信给我如何做! Im open for learning new stuff btw sorry for my bad english :3 (edit: When i guess the right number, it still says "Nope!" and i have to guess another number.) 我开放学习新东西btw抱歉我的英语不好:3(编辑:当我猜对了正确的号码,它仍然说“不!”而且我不得不猜猜另一个号码。)

This looks like Python3. 这看起来像Python3。 If so, use guess = int(input("Guess: ")) instead. 如果是这样,请改用guess = int(input("Guess: "))

In Python3 input() returns a string and you're comparing that string to an integer which will never work. 在Python3中, input()返回一个字符串,你将该字符串与一个永远不会起作用的整数进行比较。 So convert the return value of input() to an integer to be sure you're comparing apples to apples. 因此,将input()的返回值转换为整数,以确保您将苹果与苹果进行比较。

You need to put int in front of input, so: 你需要在输入前放置int,所以:

guess = int(input("Guess: "))

This will turn guess into an integer, so the code recognises it. 这会将猜测变为整数,因此代码会识别它。

The input() command returns a string and a string is not equal to a number ( "3" == 3 evaluates to false ). input()命令返回一个字符串,字符串不等于数字( "3" == 3计算结果为false )。 You can use the int(...) function to turn a string (or floating-point number) into an integer. 您可以使用int(...)函数将字符串(或浮点数)转换为整数。

I'm assuming you're using Python 3.x given that print is a function. 我假设您正在使用Python 3.x,因为print是一个函数。 If you were using Python 2.x, you should use raw_input() instead, as input() causes the interpreter to treat what was entered as Python code and execute it (like the eval(...) function does). 如果您使用的是Python 2.x,则应该使用raw_input() ,因为input()会使解释器将输入的内容视为Python代码并执行它(就像eval(...)函数那样)。

In 99.999 % of all cases, you do not want to execute user input. 在99.999%的情况下,您希望执行用户输入。 ;-) ;-)

Another rather major thing your program needs is to prompt the user so they know what they will be doing with your program exactly. 您的程序需要的另一个重要的事情是提示用户,以便他们知道他们将对您的程序做些什么。 I've added the prompt accordingly. 我已相应地添加了提示。

import random

print ("Hello. We are going to be playing a guessing game where you guess a random number from 1-10. You get three guesses.")
number = random.randint(1, 10)
# print (number) #<- To test what number im supposed to write.
guesses = 3
while guesses > 0: # When you have more than 0 guesses -> guess another number
    guess = input("Enter your guess: ")

    if guess == number : # If guess is equal to number -> end game
        print ('Your guess is right!.')
        break


    if guess != number : # If not number -> -1 guess
        print ("Nope!")
        guesses -= 1

    if guesses == 0: #If all 3 guesses are made -> end game
        print("That was it! Sorry.")
        print(number, "was the right answer!")

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

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