所以我刚刚编写了这个真正基本的脚本,只需要输入名称和值,然后将它们放入字典中并在之后显示它们,没什么,但我面临一些问题,我认为问题来自于add()函数,但我不确定。就像我说的那样我是新手......所以请运行代码n给我所有的批评......我需要它。谢谢 代码:
from time import sleep #To set delay time in the code or portions of the code
def dict_create():
# Creates the _info dictionary by entering the _name keys & _score values
_info={}
_info[_name]=_score
print ('\n',_subject, 'class students names and scores',_info.items())
def reject():
# Creates gateway for more() & dict() exec.
confirm=input(" \nYOU WILL NO LONGER BE ABLE TO ADD MORE NAMES \n\n ['yes' to quit or 'no' to continue] :")
if confirm=='no':
print ('\nBelow are the names and scores of all students offering ' + _subject )
print ('\nProcessing...')
sleep(1)
dict_create()
_more()
elif confirm=='yes':
print ('\n',_subject, 'class students names and scores',_info.items())
quit
else:
print('\nInvalid input')
reject()
def add():
# Creates gateway for _info.dict.update...[This seems to be the problem]
_add=input('\n [+] Add more names [Yes or No] :')
name=str(input ('\n [+] Please input students name :'))
score=int(input ('\n [+] Please input students score :'))
sleep(1)
# Update process for student.dict
repeat={}
repeat[name]=score
_info.update(repeat)
_more()
if _add=='yes':
_more()
elif _add=='no':
reject()
else:
print('\nSorry, invalid input, NOTE INPUT IS CASE SENSITIVE')
add()
def _more():
# Creates gateway for 'more' validation
if more=='no':
reject()
elif more=='yes':
add()
else:
print('\nInvalid input.Please note your input is case sensitive.')
_more()
# __init__
print('\nTHIS SCRIPTS GROUPS ALL NAMES & SCORES OF STUDENTS IN A CLASS')
_subject=input('\n [+] Input subject :')
_name=input('\n [+] Input students name :')
_score=float(input('\n [+] input students score :'))
print ('\nProcessing...')
sleep(1)
_info={}
_info[_name]=_score
more=input('\n [+] Do you want add more names [yes or no] :')
dict_create()
_more()
谢谢
答案 0 :(得分:0)
我重写了您的代码并进行了一些更改。首先,我使用raw_input()
而不是input()
,因为input()
指的是变量或类似的东西,但不会返回字符串。 raw_input()
返回一个字符串......所以,我纠正了你的一些函数以避免错误(例如,当我被问到是否要输入另一个名字时,我输入了#34; no" he问我另一个名字和分数。)
好的,这是你的修改代码:
from time import sleep #To set delay time in the code or portions of the code
def dict_create():
# Creates the _info dictionary by entering the _name keys & _score values
_info={}
_info[_name]=_score
print ('\n',_subject, 'class students names and scores',_info.items())
def reject():
# Creates gateway for more() & dict() exec.
confirm=raw_input(" \nYOU WILL NO LONGER BE ABLE TO ADD MORE NAMES \n\n ['yes' to quit or 'no' to continue] :")
if confirm=='no':
print ('\nBelow are the names and scores of all students offering ' + _subject )
print ('\nProcessing...')
sleep(1)
dict_create()
_more()
elif confirm=='yes':
print ('\n',_subject, 'class students names and scores',_info.items())
quit
else:
print('\nInvalid input')
reject()
def add():
# Creates gateway for _info.dict.update...[This seems to be the problem]
_add=raw_input('\n [+] Add more names [Yes or No] :')
name=str(raw_input ('\n [+] Please input students name :'))
score=int(raw_input ('\n [+] Please input students score :'))
sleep(1)
# Update process for student.dict
repeat={}
repeat[name]=score
_info.update(repeat)
_more()
if _add=='yes':
_more()
elif _add=='no':
reject()
else:
print('\nSorry, invalid input, NOTE INPUT IS CASE SENSITIVE')
add()
def _more():
# Creates gateway for 'more' validation
more=raw_input('\n [+] Do you want add more names [yes or no] :')
if more=='no':
reject()
elif more=='yes':
add()
else:
print('\nInvalid input.Please note your input is case sensitive.')
_more()
# __init__
print('\nTHIS SCRIPTS GROUPS ALL NAMES & SCORES OF STUDENTS IN A CLASS')
_subject=raw_input('\n [+] Input subject :')
_name=raw_input('\n [+] Input students name :')
_score=float(raw_input('\n [+] input students score :'))
print ('\nProcessing...')
sleep(1)
_info={}
_info[_name]=_score
dict_create()
_more()