简体   繁体   English

如何修复'ValueError:以10为基数的int()无效文字:'

[英]How to fix 'ValueError: invalid literal for int() with base 10:'

I want to write a code that should get inputs like below: 我想编写一个代码,应获得如下输入:

4
n hgh hjhgj jhh
1
jghj
3

code should get an integer n and after that get n'th strings. 代码应该得到一个整数n,然后得到第n个字符串。

i =0
A=[[],[],[]]
while i < 3:
    j=0
    n=int(input())
    while j<n:
        A[i].append((input()))
        j+=1     
    i+=1

I expect it runs but has an Error like this: invalid literal for int() with base 10: 'hj jhg hj' I can't understand this problem because n is an integer and A has strings and they have nothing to do with each other! 我希望它可以运行,但是会出现这样的错误:int()的无效字面量以10为底:'hj jhg hj'我无法理解此问题,因为n是整数且A具有字符串,并且它们与每个字符串都不相关其他! Please help why it happen, and how can I fix it? 请帮助为什么会发生,如何解决?

You are giving your inputs incorrectly, your code expects you to give the strings in different inputs, not in one line. 您错误地输入了您的输入,您的代码期望您将字符串输入到不同的输入中,而不是一行中。 To make sure what the input is asking, you could give a text to input so it shows in console: 为了确保输入的内容是正确的,您可以input文本,使其显示在控制台中:

i =0
A=[[],[],[]]
while i < 3:
    j=0
    n=int(input("n: "))
    while j<n:
        A[i].append((input("> ")))
        j+=1     
    i+=1
print(A)

This gives: 这给出:

n: 4
> n
> hgh
> hjhgj
> jhh
n: 1
> jghj
n: 3
> a
> b
> c
[['n', 'hgh', 'hjhgj', 'jhh'], ['jghj'], ['a', 'b', 'c']]

Also, when you know how many times your loop is going to iterate, instead of using while you could do for , kinda like this: 另外,当您知道循环将要迭代多少次时,而不是while可以做的情况下for ,就像这样:

A = []
for i in range(3):
    n = int(input("n: "))
    temp = []
    for j in range(n):
        temp.append(input("> "))
    A.append(temp)
print(A)

It gives you the same result ;) 它给您相同的结果;)


EDIT: 编辑:

Following assumption from @kabanus, if you actually want that kind of input you need to split the given string: 根据@kabanus的假设,如果您实际上想要那种输入,则需要分割给定的字符串:

A = []
for i in range(3):
    n = int(input("n: "))
    while True:
        words = input("> ").split()
        if len(words) == n:
          break
        print(f"You gave {len(words)} words, you must give {n} words! Try again.")
    A.append(words)
print(A)

This gives: 这给出:

n: 4
> n hgh hjhgj jhh
n: 1
> jghj
n: 3
> a b c
[['n', 'hgh', 'hjhgj', 'jhh'], ['jghj'], ['a', 'b', 'c']]

Added a while loop to keep asking and a little message if the incorrect number of words is given. 添加了while循环以继续询问,并提示一些错误的单词数。

暂无
暂无

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

相关问题 如何在我的代码中使用基数10修复“ValueError:int()的无效文字:&#39;&#39;” - How to fix the “ ValueError: invalid literal for int() with base 10: ' ' ” in my code 如何解决这个问题:ValueError:int() 的无效文字,基数为 10: - How to fix this: ValueError: invalid literal for int() with base 10: 如何修复 ValueError:int() 的无效文字,基数为 10:''? - How do i fix ValueError: invalid literal for int() with base 10: ''? 如何使用基数 10 修复 int() 的无效文字:&#39;&#39;? - How to fix invalid literal for int() with base 10: ''? 如何修复“ValueError: invalid literal for int() with base 10: &#39;&#39;发生。无论何时需要pyobjc,包括安装pyobjc - How to fix "ValueError: invalid literal for int() with base 10: '' occuring. whenever pyobjc is needed including installing pyobjc 我该如何解决这个问题:ValueError invalid literal for int() with base 10: ''? Python - How can I fix this: ValueError invalid literal for int() with base 10: ''? Python ValueError:int() 的无效文字,基数为 10:&#39;26.02.2018&#39; - ValueError: invalid literal for int() with base 10: '26.02.2018' 第8行:ValueError:int()以10为底的无效文字:“&#39; - Line 8: ValueError: invalid literal for int() with base 10: ' ' ValueError:int()以10为基的无效文字:“ skip” - ValueError: invalid literal for int() with base 10: 'skip' ValueError:以10为底的int()的无效文字:&#39;&#39; - ValueError: invalid literal for int() with base 10: ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM