简单的raw_input和条件 [英] Simple raw_input and conditions

查看:217
本文介绍了简单的raw_input和条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了简单的代码:

name = raw_input("Hi. What's your name? \nType name: ")
age = raw_input("How old are you " + name + "? \nType age: ")

if age >= 21
    print "Margaritas for everyone!!!"
else:
    print "NO alcohol for you, young one!!!"

raw_input("\nPress enter to exit.")

It工作得很好,直到我得到'if'语句...它告诉我,我使用的语法无效。

It works great until I get to the 'if' statement... it tells me that I am using invalid syntax.

我正在努力学习如何使用Python,并且已经对代码进行了大量的讨论,但我无法弄清楚我做错了什么(可能是非常基本的。

I am trying to learn how to use Python, and have messed around with the code quite a bit, but I can't figure out what I did wrong (probably something very basic).

推荐答案

它应该是这样的:

name = raw_input("Hi. What's your name? \nType name: ")
age = raw_input("How old are you " + name + "? \nType age: ")
age = int(age)

if age >= 21:
    print "Margaritas for everyone!!!"
else:
    print "NO alcohol for you, young one!!!"

raw_input("\nPress enter to exit.")

你错过了冒号。此外,你应该将年龄从字符串转换为int。

You were missing the colon. Also, you should cast age from string to int.

希望这会有所帮助!

这篇关于简单的raw_input和条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆