Python 3.x 如何在python中一行读取两个变量integer和char
在python 3.5中,如何输入多个变量,比如integer、char和integer?当我尝试如下所示(python代码)时,它会给我一个错误,如 回溯(最后一次调用):文件“F:/Programming/424-Integer Inquiry.py”,第3行,在x=int(input())值中错误:无效 以10为基数的int()的文字:“300+3” 就像在c中:Python 3.x 如何在python中一行读取两个变量integer和char,python-3.x,Python 3.x,在python 3.5中,如何输入多个变量,比如integer、char和integer?当我尝试如下所示(python代码)时,它会给我一个错误,如 回溯(最后一次调用):文件“F:/Programming/424-Integer Inquiry.py”,第3行,在x=int(input())值中错误:无效 以10为基数的int()的文字:“300+3” 就像在c中: #include<stdio.h> int main() { int a,b; char ch
#include<stdio.h>
int main()
{
int a,b;
char ch;
scanf("%d %c %d", &a,&ch,&d);
return 0;
}
但是,它不起作用。
我可以用C、C++或java来做这件事,但是我怎么能在Python 3.5中这样做??< /P> < P> Python没有<代码> char < /C> >类型,只有字符串。
# These are both the same
s1 = "Hello"
s2 = 'Hello'
# So are these
s3 = "a"
s4 = 'a'
chr
实际上是一个类似于int
的内置函数,它将整数转换为其ascii等效值
>>> chr(65)
'A'
此外,您收到的错误是由于您试图将“300+3”
转换为int
函数无法计算的整数
您将要看到的是使用
str.split
方法分割输入
inp = input("Enter words: ")
values = inp.split()
print(values) # print list of words from input
print(values[0]) # print first word
# Output
Enter words: Hello World! How are you?
['Hello', 'World!', 'How', 'are', 'you?']
Hello
您可以为变量提供任何输入,下面是代码
value=input()
list1=list()
print("You have entered:-",value)
for number in value.split():
list1.append(number)
print(list1)
此代码的输出:
123 you have enter 456
You have entered:- 123 you have enter 456
['123', 'you', 'have', 'enter', '456']
现在你想做什么就做什么
123 you have enter 456
You have entered:- 123 you have enter 456
['123', 'you', 'have', 'enter', '456']