国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

在Python中使用json模塊的入門問題

瀏覽:105日期:2022-08-23 08:49:33

問題描述

#coding:GBKimport jsondef getstoredname(): filename = ’username.json’ try:with open(filename) as f: username = json.load(f) except:return None else:return usernamedef getnewname(): username = input('What is your name? ') filename = ’username.json’ with open(filename,’a+’) as f:json.dump(username,f) return username def greetuser(): username = getstoredname() if username:print('Welcome back, ' + username + '!') else:username = getnewname()print ('We’ll remember you when you come back, ' + username + '!')greetuser()

在Python中使用json模塊的入門問題這個(gè)問題應(yīng)該怎么改代碼?

問題解答

回答1:

def greetuser(): username = getstoredname() if username and input('Is that your name: ' + username + ' (y/n)')=='y': print('Welcome back, ' + username + '!') else:username = getnewname()print ('We’ll remember you when you come back, ' + username + '!')

我回答過的問題: Python-QA

回答2:

import json’’’如果以前存儲(chǔ)了用戶名,就加載它,并詢問是否為該用戶的用戶名,否則,就提示用戶輸入用戶名并存儲(chǔ)它 。’’’filename = ’username.json’try:

with open(filename) as f_obj: username = json.load(f_obj) if input(’Is that your name: ’ + username +’?’ + ’ (y/n) n’)==’y’:print('Welcom back,%s!' %username) else:username = input(’What is your name?n’)with open(filename,’w’) as f_obj: json.dump(username,f_obj) print('We’ll remember you when you come back,%s!' % username)

except FileNotFoundError:

username = input(’What is your name?n’)with open(filename,’w’) as f_obj: json.dump(username,f_obj) print('We’ll remember you when you come back,%s!' % username)

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 桦川县| 会东县| 湘潭县| 泽普县| 龙州县| 宁化县| 水城县| 安福县| 南京市| 周口市| 阿城市| 菏泽市| 夏河县| 昌宁县| 交口县| 德江县| 镶黄旗| 泗洪县| 临泉县| 雷州市| 旅游| 东平县| 田阳县| 麻江县| 平凉市| 彩票| 呼图壁县| 固安县| 盈江县| 溆浦县| 弋阳县| 民县| 广灵县| 布拖县| 瓮安县| 容城县| 剑河县| 江津市| 南木林县| 旅游| 于田县|