Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Python变量会被分配到错误的值?_Python_Debugging - Fatal编程技术网

为什么Python变量会被分配到错误的值?

为什么Python变量会被分配到错误的值?,python,debugging,Python,Debugging,问我面临的问题的原因:我的变量“拼音”有时不会更新。我将提供代码,然后给出一个bug示例: 代码用途:输出乌克兰字母表中的字母,用户必须输入给定的字母拼音。 import random import sys print('Welcome to the program. You will be provided with random Ukrainian letters, and it is your job to input the correct phonetic pron

问我面临的问题的原因:我的变量“拼音”有时不会更新。我将提供代码,然后给出一个bug示例:

代码用途:输出乌克兰字母表中的字母,用户必须输入给定的字母拼音。

   import random
   import sys

   print('Welcome to the program. You will be provided with random Ukrainian letters, and it is your job to input the correct phonetic pronounciation of the given letter. Would you like to continue?')

   while True:
       begin = input('type yes or no: ')
       if begin == 'yes':
           break
       if begin == 'no':
           sys.exit()

   alphabet = ["А", "Б", "В", "Г", "Ґ", "Д", "Е", "Є", "Ж", "З", "И", "І", "Ї",
        "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "ь", "Ю", "Я"] 

   phonetic = 0


#randomly grab from a list (possibly using choice()) and ask user for input
print("type 'end' to end the program")
while True:
    currentLetter = random.choice(alphabet)
    if currentLetter == alphabet[0]:
        phonetic == 'ah'
    elif currentLetter == alphabet[1]:
        phonetic == 'beh'
    elif currentLetter == alphabet[2]:
        phonetic == 'veh'
    elif currentLetter == alphabet[3]:
        phonetic == 'heh'
    elif currentLetter == alphabet[4]:
        phonetic == 'geh'
    elif currentLetter == alphabet[5]:
        phonetic == 'deh'
    elif currentLetter == alphabet[6]:
        phonetic == 'eh'
    elif currentLetter == alphabet[7]:
        phonetic == 'yeh'
    elif currentLetter == alphabet[8]:
        phonetic == 'zheh'
    elif currentLetter == alphabet[9]:
        phonetic == 'zeh'
    elif currentLetter == alphabet[10]:
        phonetic == 'ih'
    elif currentLetter == alphabet[11]:
        phonetic == 'ee'
    elif currentLetter == alphabet[12]:
        phonetic == 'yih'
    elif currentLetter == alphabet[13]:
        phonetic == 'yot'
    elif currentLetter == alphabet[14]:
        phonetic == 'kah'
    elif currentLetter == alphabet[15]:
        phonetic == 'le'
    elif currentLetter == alphabet[16]:
        phonetic = 'muh'
    elif currentLetter == alphabet[17]:
        phonetic = 'nuh'
    elif currentLetter == alphabet[18]:
        phonetic = 'oh'
    elif currentLetter == alphabet[19]:
        phonetic = 'peh'
    elif currentLetter == alphabet[20]:
        phonetic = 'ruh'
    elif currentLetter == alphabet[21]:
        phonetic = 'suh'
    elif currentLetter == alphabet[22]:
        phonetic = 'te'
    elif currentLetter == alphabet[23]:
        phonetic = 'ooh'
    elif currentLetter == alphabet[24]:
        phonetic = 'fuh'
    elif currentLetter == alphabet[25]:
        phonetic = 'kha'
    elif currentLetter == alphabet[26]:
        phonetic = 'tse'
    elif currentLetter == alphabet[27]:
        phonetic = 'cheh'
    elif currentLetter == alphabet[28]:
        phonetic = 'shah'
    elif currentLetter == alphabet[29]:
        phonetic = 'shcha'
    elif currentLetter == alphabet[30]:
        phonetic = 'myakih znak'
    elif currentLetter == alphabet[31]:
        phonetic = 'yooh'
    elif currentLetter == alphabet[32]:
        phonetic = 'yah'
        
    print('Type in the phonetic spelling of ' + currentLetter + ':')
    userInput = input()
    if userInput == phonetic:
        print('nice work!')
    elif userInput == 'end':
        print('Thanks for playing!')
        break
    else :
        print('Your input was incorrect. The correct phonetic spelling is ' + phonetic + '.')
错误示例:拼音变量有时指定了错误的值(发音)。

   import random
   import sys

   print('Welcome to the program. You will be provided with random Ukrainian letters, and it is your job to input the correct phonetic pronounciation of the given letter. Would you like to continue?')

   while True:
       begin = input('type yes or no: ')
       if begin == 'yes':
           break
       if begin == 'no':
           sys.exit()

   alphabet = ["А", "Б", "В", "Г", "Ґ", "Д", "Е", "Є", "Ж", "З", "И", "І", "Ї",
        "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "ь", "Ю", "Я"] 

   phonetic = 0


#randomly grab from a list (possibly using choice()) and ask user for input
print("type 'end' to end the program")
while True:
    currentLetter = random.choice(alphabet)
    if currentLetter == alphabet[0]:
        phonetic == 'ah'
    elif currentLetter == alphabet[1]:
        phonetic == 'beh'
    elif currentLetter == alphabet[2]:
        phonetic == 'veh'
    elif currentLetter == alphabet[3]:
        phonetic == 'heh'
    elif currentLetter == alphabet[4]:
        phonetic == 'geh'
    elif currentLetter == alphabet[5]:
        phonetic == 'deh'
    elif currentLetter == alphabet[6]:
        phonetic == 'eh'
    elif currentLetter == alphabet[7]:
        phonetic == 'yeh'
    elif currentLetter == alphabet[8]:
        phonetic == 'zheh'
    elif currentLetter == alphabet[9]:
        phonetic == 'zeh'
    elif currentLetter == alphabet[10]:
        phonetic == 'ih'
    elif currentLetter == alphabet[11]:
        phonetic == 'ee'
    elif currentLetter == alphabet[12]:
        phonetic == 'yih'
    elif currentLetter == alphabet[13]:
        phonetic == 'yot'
    elif currentLetter == alphabet[14]:
        phonetic == 'kah'
    elif currentLetter == alphabet[15]:
        phonetic == 'le'
    elif currentLetter == alphabet[16]:
        phonetic = 'muh'
    elif currentLetter == alphabet[17]:
        phonetic = 'nuh'
    elif currentLetter == alphabet[18]:
        phonetic = 'oh'
    elif currentLetter == alphabet[19]:
        phonetic = 'peh'
    elif currentLetter == alphabet[20]:
        phonetic = 'ruh'
    elif currentLetter == alphabet[21]:
        phonetic = 'suh'
    elif currentLetter == alphabet[22]:
        phonetic = 'te'
    elif currentLetter == alphabet[23]:
        phonetic = 'ooh'
    elif currentLetter == alphabet[24]:
        phonetic = 'fuh'
    elif currentLetter == alphabet[25]:
        phonetic = 'kha'
    elif currentLetter == alphabet[26]:
        phonetic = 'tse'
    elif currentLetter == alphabet[27]:
        phonetic = 'cheh'
    elif currentLetter == alphabet[28]:
        phonetic = 'shah'
    elif currentLetter == alphabet[29]:
        phonetic = 'shcha'
    elif currentLetter == alphabet[30]:
        phonetic = 'myakih znak'
    elif currentLetter == alphabet[31]:
        phonetic = 'yooh'
    elif currentLetter == alphabet[32]:
        phonetic = 'yah'
        
    print('Type in the phonetic spelling of ' + currentLetter + ':')
    userInput = input()
    if userInput == phonetic:
        print('nice work!')
    elif userInput == 'end':
        print('Thanks for playing!')
        break
    else :
        print('Your input was incorrect. The correct phonetic spelling is ' + phonetic + '.')

谢谢你阅读和考虑我的问题

上面的代码片段有问题

#randomly grab from a list (possibly using choice()) and ask user for input
print("type 'end' to end the program")
while True:
    currentLetter = random.choice(alphabet)
    if currentLetter == alphabet[0]:
        phonetic == 'ah'

    ...

    elif currentLetter == alphabet[15]:
        phonetic == 'le'
=
用于赋值,
=
用于比较相等值


你需要
=
而不是
=

试试这个代码,我对它做了一些小改动。将项目存储在拼音变量中时出错。如果要在变量中存储任何内容,请使用赋值运算符

ie. phonetic='ssh'
这里错误地使用了比较运算符('=')而不是赋值运算符

import random
import sys

print('Welcome to the program. You will be provided with random Ukrainian letters, and it is your job to input the correct phonetic pronounciation of the given letter. Would you like to continue?')

while True:
       begin = input('type yes or no: ')
       if begin == 'yes':
           break
       if begin == 'no':
           sys.exit()

alphabet = ["А", "Б", "В", "Г", "Ґ", "Д", "Е", "Є", "Ж", "З", "И", "І", "Ї",
        "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "ь", "Ю", "Я"] 

phonetic =''

#randomly grab from a list (possibly using choice()) and ask user for input
print("type 'end' to end the program")
while True:
    currentLetter = random.choice(alphabet)
    if currentLetter == alphabet[0]:
        phonetic = 'ah'
    elif currentLetter == alphabet[1]:
        phonetic = 'beh'
    elif currentLetter == alphabet[2]:
        phonetic = 'veh'
    elif currentLetter == alphabet[3]:
        phonetic = 'heh'
    elif currentLetter == alphabet[4]:
        phonetic = 'geh'
    elif currentLetter == alphabet[5]:
        phonetic = 'deh'
    elif currentLetter == alphabet[6]:
        phonetic = 'eh'
    elif currentLetter == alphabet[7]:
        phonetic = 'yeh'
    elif currentLetter == alphabet[8]:
        phonetic = 'zheh'
    elif currentLetter == alphabet[9]:
        phonetic = 'zeh'
    elif currentLetter == alphabet[10]:
        phonetic ='ih'
    elif currentLetter == alphabet[11]:
        phonetic = 'ee'
    elif currentLetter == alphabet[12]:
        phonetic = 'yih'
    elif currentLetter == alphabet[13]:
        phonetic = 'yot'
    elif currentLetter == alphabet[14]:
        phonetic = 'kah'
    elif currentLetter == alphabet[15]:
        phonetic = 'le'
    elif currentLetter == alphabet[16]:
        phonetic = 'muh'
    elif currentLetter == alphabet[17]:
        phonetic = 'nuh'
    elif currentLetter == alphabet[18]:
        phonetic = 'oh'
    elif currentLetter == alphabet[19]:
        phonetic = 'peh'
    elif currentLetter == alphabet[20]:
        phonetic = 'ruh'
    elif currentLetter == alphabet[21]:
        phonetic = 'suh'
    elif currentLetter == alphabet[22]:
        phonetic = 'te'
    elif currentLetter == alphabet[23]:
        phonetic = 'ooh'
    elif currentLetter == alphabet[24]:
        phonetic = 'fuh'
    elif currentLetter == alphabet[25]:
        phonetic = 'kha'
    elif currentLetter == alphabet[26]:
        phonetic = 'tse'
    elif currentLetter == alphabet[27]:
        phonetic = 'cheh'
    elif currentLetter == alphabet[28]:
        phonetic = 'shah'
    elif currentLetter == alphabet[29]:
        phonetic = 'shcha'
    elif currentLetter == alphabet[30]:
        phonetic = 'myakih znak'
    elif currentLetter == alphabet[31]:
        phonetic = 'yooh'
    elif currentLetter == alphabet[32]:
        phonetic = 'yah'
        
    print('Type in the phonetic spelling of ' + currentLetter + ':')
    userInput = input()
    if userInput == phonetic:
        print('nice work!')
    elif userInput == 'end':
        print('Thanks for playing!')
        break
    else :
        print('Your input was incorrect. The correct phonetic spelling is ' + phonetic + '.')

如果你使用字典,每个字母的发音都直接指定,那么代码就会简单得多。
拼音=='zeh'
不是你指定字母的方式谢谢上面的评论!对于OPs福利,也许您可以包括how=和==差异。谢谢您的回复!非常感谢。非常感谢您的回复!非常感谢。