[解决]如何在没有错误的情况下将int转换为字符串? [英] [Solved] how do I convert an int to a string without an error?

查看:75
本文介绍了[解决]如何在没有错误的情况下将int转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案:

我将比较更改为:

Solution:
I changed the comparison to:

if int(guess[i]) == number[i]:





完整程序:



Full Program:

import random

def generateNumbers():
    numbers = [];
    i = 0
    while i < 4:
        newNumber = random.randint(0, 9)
        if numbers.count(newNumber) == 0:
            numbers.append(newNumber)
            i += 1
    return numbers

def checkValidity(guess):
    if len(guess) != 4:
        print("Your guess does not contain four numbers.")
        return False
    if guess.isdigit() != True:
        print("Your guess is not a number.")
        return False
    i = 0
    while i < 4:
        if guess.count(guess[i]) != 1:
            print("Your number contains a duplicate.")
            return False
        i += 1
    return True

def checkMatch(guess, number):
    bulls = 0
    cows = 0
    i = 0
    while i < 4:
        if guess[i] == number[i]:
            bulls += 1
        elif guess[i] in number:
            cows += 1
        i += 1
    print("Your number contains " + str(bulls) + " and " + str(cows) + " cows.")
    if bulls == 4:
        return True
    else:
        return False

number = generateNumbers()
print(number)
guess = ""
guesses = 0
while guess != "exit":
    print("Enter your guess or 'exit'.")
    guess = input("Guess: ").lower()
    if guess == "exit":
        break
    if checkValidity(guess):
        guesses += 1
        if checkMatch(int(guess), number):
            print("Well done! You guessed the number in " + str(guesses) + " tries.")





您好,我想在以下代码中将两个整数进行比较:



Hi there, I am want to compare two ints together in the following code:

if guess[i] == number[i]:

但是,我收到以下错误:

However, I am getting the following error:

TypeError: 'int' object is not subscriptable





我尝试过:



- 将变量guess转换为代码不同位置的int。

- 根本不转换它。



What I have tried:

- Converting the variable "guess" into an int in different places of the code.
- Not converting it at all.

推荐答案

guess 不是[] - 所以它可以;与下标索引一起使用。

尝试:

guess is not an [] - so it can;t be used with a subscript index.
Try:
if guess == number[i]:


Quote:

这会破坏原始代码的目的。

That would defeat the purpose of the original code.



对解决方案1进行更正,并且使用调试器来查看代码正在执行的操作。在某个地方,你的代码没有达到预期的效果。



如果你不理解你的代码在做什么或为什么它做了它的作用,答案就是< b>调试器。

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你至。当代码没有达到预期的效果时,你就会接近一个错误。


Do the correction of solution 1, and use the debugger to see what your code is doing. Somewhere, your code is not doing what you expect.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于[解决]如何在没有错误的情况下将int转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆