我试图在python中输入字符串,你能告诉我吗

时间:2017-02-09 10:14:10

标签: python

我是编程新手。如果我在Python中使用x = int(input("Enter the no.")) if (x>5): print (x ,'Is greater than 5') elif (x==str):enter code here print('Incorrect input') elif (x==5): print (x ,'Is equal to 5') elif (x<5): print (x ,'Is less than 5') else: print (x ,'none of the above!') 语句输入字符串,如何收到错误消息。

cbVmUserName.DataSource = ds_4.Tables["VmUserNames"];
cbVmUserName.ValueMember = "vm_username";
cbVmUserName.SelectedIndex = 0;

1 个答案:

答案 0 :(得分:0)

我不知道你是如何学习Python的,但要么你的来源很糟糕,要么你已经超前了。

<强>问题

  1. 将您的输入转换为int
    在Python 3中,您通过input()函数收到的任何用户输入都被视为string。因此,当您执行x = input()时,x的类型为string。通过使用input()函数包装int()函数,您可以将此输出(应该是string)转换为int

  2. 检查条件时,请将x直接与str进行比较,工作。 strstring class,而x是其实例。如果你想比较两者,你显然必须使用type()函数 - type(x) == str