Python 为多个输入引发自定义值错误

问题描述

我有一个需要两个值的输入

       function BarangayInput(props) {
  const [inputAddress,setInputAddress] = useState(
    props.completeAddress
  );
  const [showList,setShowList] = useState(false);
  const [barangayList,setBarangayList] = useState([]);

  function onChange(e) {
    props.onChangeHandler(e,"barangay");
    getBarangayAPI();
  }

  helper.log("[Barangay Input]: render");
  helper.log("[Barangay Input]: render",barangayList);

  return (
    <div style={{ margin: "0" }} className={style.barangay}>
   
      <input
        style={{ width: "100%" }}
        className={style.inputstyle + " " + style.barangayinput}

        disabled={!props.isEditable}
        onChange={onChange}
        value={inputAddress}
        id={8}
        placeholder="Ex: Santolan,Pasig City,1610"
        //value={props.partnerProfile.email_address}
      />
      {showList ? <div className={style.listContainer}>{list}</div> : null}
    </div>
  );
}

const mapStatetoProps = (state) => {
  return {
    barangay: state.profileAndDetails.barangay,completeAddress:
      state.profileAndDetails.partnerProfileAndDetails.barangay +
      " " +
      state.profileAndDetails.partnerProfileAndDetails.city +
      " " +
      state.profileAndDetails.partnerProfileAndDetails.province +
      " " +
      state.profileAndDetails.partnerProfileAndDetails.zip_code,barangay: state.profileAndDetails.partnerProfileAndDetails.barangay,};
};

我注意到,如果我只输入一个值,我的程序就会终止并引发 input1,input2 = input("Enter two types of pizza separate each with a space: ").split() 在这种情况下,我是否能够引发我自己的自定义 ValueError 而不是终止我的程序,我可以让它简单地重新提示用户再次输入它吗?我想我可以用 ValueError: not enough values to unpack (expected 2,got 1) 循环做到这一点吗?

这是我的尝试。

while

解决方法

当您使用多重赋值语法 a,b = <some list or tuple> 时,如果左侧变量名的数量与可迭代对象的长度不匹配,则会立即抛出 ValueError

更简洁的方法是:

values = input("Enter two types of pizza separate each with a space: ").split()
if len(values) == 2:
    input1,input2 = values
    ...
else:
    print("You must enter two types separated by a space!")

如果你想重复提示直到输入有效的输入,你可以在while循环中显示提示:

message = "Enter two types of pizza separated by a space: "
values = input(message).split()

# Block until exactly two words are entered
while len(values) != 2:
    print("You must enter two types of pizza separated by a space!")
    values = input(message).split()

# After while loop completes,we can safely unpack the values
input1,input2 = values
...
,

使用 while 循环肯定可以解决问题:

input1,input2 = None,None
while input1 is None and input2 is None:
    try: 
        input1,input2 = input("Enter two types of pizza separate each with a space: ").split()
    except ValueError as err:
        print("Ensure that you enter two types separate each with a comma")

注意:作为以前的用户发布的,没有必要提出自己的错误,因为当输入不正确时会自动提出。

此外,不要忘记在条件语句中使用 == 而不仅仅是 =。 :-)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...