日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

input python2.7_python 中的input

發布時間:2025/3/21 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 input python2.7_python 中的input 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

渣渣之路。

一、 在python編程初學者指南中的第六章、使用參數和返回值的例子中:

# -*- coding: utf-8 -*-

def display(message):

print message

def give_me_five():

five = 5

return five

def ask_yes_no(question):

"""

Ask a yes or no questions.

"""

response = None

while response not in ('y', 'n'):

response = input(question).lower()

return response

display("here is a message for you\n")

number = give_me_five()

print "Here's what I got from give_me_five():", number

answer = ask_yes_no("\nPlease enter 'y' or 'n': ")

print "Thank you for entering:", answer

發現自己在pycharm下輸入的:y會報錯

Please enter 'y' or 'n': y

Traceback (most recent call last):

File "E:/Project/actneed411/furion/static/js/template/testa.py", line 25, in

answer = ask_yes_no("\nPlease enter 'y' or 'n': ")

File "E:/Project/actneed411/furion/static/js/template/testa.py", line 19, in ask_yes_no

response = input(question).lower()

File "", line 1, in

NameError: name 'y' is not defined

但是,輸入:'y'或者"y"卻是對的:

here is a message for you

Here's what I got from give_me_five(): 5

Please enter 'y' or 'n': 'y' "y"

Thank you for entering: y

二、探究python中的input【1】

由【1】中的文檔中,python2.7中輸入函數有兩種:

1、raw_input():返回的是字符串--string類型,即輸入:1+2,返回顯示的是:"1+2"

2、input():返回的是數值類型,int,float等,即輸入:1+2,返回顯示的是:3

而在python3中輸入只有一種:

input():返回的是字符串--string類型,沒有數值類型了相當于原來的raw_input()

【2】以前有分raw_input和input, raw_input讀什么東西都是string, input會解析數據,

版本3合并了raw_input和input, 只能讀到string了, 原先的可解析版本不安全,

如果要讀到數值,使用類型轉換:

a = int(input("a="))

恰好數中使用的是python是python3,這樣就能解釋通上邊的問題了。

------------420 三--

總結

以上是生活随笔為你收集整理的input python2.7_python 中的input的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。