类型错误> =' str'的实例之间不支持和' int'

时间:2018-02-18 01:21:46

标签: python

我已经写出了我的代码,但我一直在if personsAge >= 1上收到错误消息。

这是错误:

  

类型错误> =' str'的实例之间不支持和' int'

每次运行程序时都会发生这种情况。我不确定我做错了什么。非常感谢任何帮助。

这是我的代码:

# Purpose
''' This program classifies a person to a certain group based on their age'''
#=================================================================================
# Initialize variables(used for processing data)
ageGroup =""; #specify data as empty string
personsAge =""; #specify data as float /real
#====================================================================================

# Input Statements
fullName = str(input("<Enter your full name>"));
personsAge = str(input("<Enter your age>"));

#==========================================================================
'''nested if statements to determine a person's age group.'''

# Using the Nested If eliminates the need to check all if statements
# Once the criteria has been met, control is transferred to the statement
# following the last if Statement. However, the IF/ELSE must be aligned in same column

if (personsAge <=1) and (personsAge >0):
    ageGroup = "Infant.";

elif (personsAge >=1) and (personsAge <=13):
    ageGroup = "Child.";

elif (personsAge >=13) and (personsAge <=20):
    ageGroup = "Teenager.";

elif (personsAge >=20):
    ageGroup = "Adult.";
#====================================================================================
#Output Statements
print();
print("My name is " + fullName);
print("My age is " + personsAge);

#=================================================================
# Print Age group
print(fullName + "is an " + ageGroup);
print("=" * 80);
#===============================================================================
#End program

1 个答案:

答案 0 :(得分:2)

您正在将输入转换为字符串而不是整数。

使用import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.GeneralPath; import java.awt.geom.Line2D; import java.awt.geom.Path2D; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class Test { public static void main(String[] args) { new Test(); } public Test() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private boolean dragging = false; private Point anchor; private Point target; private Shape fakePath; private List<Shape> shapes = new ArrayList<>(25); public TestPane() { addMouseMotionListener(new MouseAdapter() { @Override public void mouseDragged(MouseEvent e) { dragging = true; target = new Point(e.getPoint()); repaint(); } @Override public void mouseMoved(MouseEvent e) { if (target != null && anchor != null) { fakePath = makePath(anchor, e.getPoint(), target); repaint(); } } }); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (anchor == null) { target = null; anchor = new Point(e.getPoint()); } } @Override public void mouseReleased(MouseEvent e) { dragging = false; } @Override public void mouseClicked(MouseEvent e) { if (anchor != null && target != null) { fakePath = null; shapes.add(makePath(anchor, e.getPoint(), target)); anchor = null; target = null; repaint(); } } }); } protected Path2D makePath(Point p1, Point p2, Point p3) { Path2D p = new GeneralPath(); p.moveTo(p1.getX(), p1.getY()); p.curveTo(p1.getX(), p1.getY(), p2.getX(), p2.getY(), p3.getX(), p3.getY()); return p; } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); g2d.setColor(Color.BLACK); for (Shape shape : shapes) { g2d.draw(shape); } if (anchor != null && target != null) { g2d.setColor(Color.GREEN); g2d.draw(new Line2D.Double(anchor, target)); } if (fakePath != null) { g2d.setColor(Color.BLUE); g2d.draw(fakePath); } g2d.dispose(); } } } 转换您的输入:

int()

然后你就可以将它与其他整数进行比较。

每次将personsAge = int(input("<Enter your age>")); 与字符串连接时,请不要忘记将personsAge整数转换为带str()的字符串。例如:

personsAge

最好在不保证用户输入属于所需类型的情况下添加错误处理。例如:

print("My age is " + str(personsAge));