python - 有什么方法可以返回我猜对的数字并在其后添加文本?

标签 python error-handling

一切正常,直到最后第二行。我找不到一种方法来返回我在一个不错的结尾句子中猜到的数字。

试图将return UserInput +“是正确的数字转换为整数,就像return int(UserInput +”是正确的数字。)-但这似乎更错误。哈哈enter code here

from random import randint

def Guess(n):
    Number=randint(0,n)
    UserInput=int(input("Insert your number here:"))
    while UserInput != randint:
        if UserInput<Number:
            print ("Your number is too small")
        if UserInput>Number:
            print ("Your number is too big")
        if UserInput==Number:
            return UserInput+" was the correct number.
        UserInput=int(input("Insert your number here:"))     

我希望在猜出正确的数字后会像:(int)是正确的数字

最佳答案

您应该将int转换为字符串以返回或打印它
使用str()进行转换
这是您的代码:

from random import randint

def Guess(n):
    Number=randint(0,n)
    UserInput=int(input("Insert your number here:"))
    while UserInput != randint:
        if UserInput<Number:
            print ("Your number is too small")
        if UserInput>Number:
            print ("Your number is too big")
        if UserInput==Number:
            print( str(UserInput)+" was the correct number.")
            break
        UserInput=int(input("Insert your number here:")) 

关于python - 有什么方法可以返回我猜对的数字并在其后添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54492913/

相关文章:

python - 对二维 numpy 数组执行分组运算

java - 我的2D数组Java代码找不到错误

c++ - 如何使用 FormatMessage 函数填充系统错误消息的 va_list?

algorithm - 基本 Reed-Solomon 纠错问题

python - 如何在 numpy 中将列添加到矩阵

python - 在Django项目中获取typeError错误

python - pandas dataframe 对列进行排序会引发索引上的 keyerror

java - 这是 Spring-Kafka 文档中有关 BatchErrorHandler 的错误吗?

bash - 从 bash 脚本中启动的命令中检索错误代码

python - 覆盖创建方法以创建另一个模型的记录