输入()

python
阅读 76 收藏 0 点赞 0 评论 0

Input
####################################
### INPUT()
####################################

# input() allows you to collect information from the user. The argument can display prompt text.

variable = input('Enter a value:')

# say user enters '7'

variable
#result: '7'

# Note that the '7' is stored as text. Therefore you can't use the number for arithmetic.
# In order to accept it as a number, use the int() or float() functions around input()

variable = int(input('Enter a value:'))

# say user enters '7'

variable
#result: 7

# A more advanced assignement of a varible, which then prints text based based on an IF ELSEIF ELSE statement.
# You need to use float() or int() to pass the value entered by the user as a numeric data type
# (thereby allowing it to be evaluated in the IF statment as equal ( == ) to either 7 or 10
variable = float(input('Enter a value for the variable: '))

if variable == 7: 
    print('Variable is 7')
#else if is shortened to 'elif'
elif variable == 10:
    print('Variable is 10')
else: 
    print('Variable is not 7 or 10')
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号