我不明白为什么我的if / elif / else陈述式无法正常运作?

时间:2019-05-05 04:30:26

标签: python python-3.x if-statement

enter image description here

我正在练习使用if / elif / else语句,如果垃圾邮件等于1,则此程序应在此打印“ Hello”;如果垃圾邮件等于2,则应打印“ Howdy”;如果其他值等于“ Greetings”,则应打印。但是由于某种原因,即使垃圾邮件等于1或2,它也会打印出“问候”。

我什至将正确的代码(来自使用Python编程自动完成无聊的事情)复制并粘贴到此练习问题上,但仍然无法正常工作。在PyCharm和常规IDLE上进行了测试。

print('Please enter a number.')
spam = input()

if spam == 1:
    print('Hello')
elif spam == 2:
    print('Howdy')
else:
    print('Greetings')

1 个答案:

答案 0 :(得分:0)

<script> var selected=select.options[select.selectedIndex].value; google.script.run .withSuccessHandler(selected)//select is supposed to be a function name .nightTargetSelection(selected); google.script.host.close(); </script> 返回一个字符串,而不是整数。

您可以将垃圾邮件转换为整数:#pragma once #include <QAbstractItemModel> #include <QString> #include <QMimedata.h> #include <Qdatastream.h> #include "Container.h" class TreeItem; class TreeModel : public QAbstractItemModel { Q_OBJECT public: TreeModel( const QString &header, const Container &data, QObject *parent = 0); ~TreeModel(); QVariant data(const QModelIndex &index, int role) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override; QModelIndex index(int row, int column, const QModelIndex &parent) const override; QModelIndex parent(const QModelIndex &parent) const override; bool insertRows(int position, int rows, const QModelIndex &parent); // bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override; Qt::ItemFlags flags(const QModelIndex &index) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; Container GetContainer(const QModelIndex &index); void SetContainer(const QModelIndex &index, Container cont); ////////////////////// Drag And Drop Actions /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Qt::DropActions supportedDropActions() const override; Qt::DropActions supportedDragActions() const override; QStringList mimeTypes() const override; QMimeData *mimeData(const QModelIndexList &indexes) const override; bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex &parent); void setupModelData(const QStringList &lines, TreeItem *parent); void removeItem(TreeItem *item); TreeItem *getItem(const QModelIndex &index) const; TreeItem *getRoot(); private: TreeItem *rootItem; }; 而不是input()

或让代码检查字符串版本:

spam = int(input())