[英]Python always getting same result
我在下面編寫的程序具有以下要求:
# Design a program that prompts the user to enter the names of two primary colors
# to mix. If the user enters anything other than red, blue or yellow, the
# program should display an error message. Otherwise the program should display
# the name of the secondary color that results.
這是我編寫的代碼-基於我之前編寫的Java程序,顯然不適合Python。
print('You will be mixing two primary colors to get a resulting color.')
print('Primary colors are blue, red and yellow \n')
red = False
blue = False
yellow = False
color1 = bool(input('Enter your first primary color: \n'))
color2 = bool(input('Enter your second primary color: \n'))
if color1 == red and color2 == blue:
print('That makes purple!')
elif color1 == blue and color2 == red:
print('That makes purple!')
elif color1 == yellow and color2 == red:
print('That makes orange!')
elif color1 == red and color2 == yellow:
print('That makes orange!')
elif color1 == blue and color2 == yellow:
print('That makes green!')
elif color1 == yellow and color2 == blue:
print('That makes green!')
else:
print('You did not enter a primary color!')
無論輸入什么顏色組合,都會得到“紫色”的結果。 該程序的邏輯哪里出了問題? 此外,當我不輸入綠色作為原色時,我得到以下信息:
Traceback (most recent call last):
File "color.py", line 19, in <module>
color1 = bool(input('Enter your first primary color: \n'))
File "<string>", line 1, in <module>
NameError: name 'green' is not defined
而不是錯誤消息“您沒有輸入原色!”
我要去哪里錯了?
編輯:這是我的新代碼,除了錯誤輸出外,它還可以工作。
print('You will be mixing two primary colors to get a resulting color.')
print('Primary colors are blue, red and yellow \n')
red = 1
blue = 2
yellow = 3
color1 = input('Enter your first primary color: \n')
color2 = input('Enter your second primary color: \n')
if color1 == 1 and color2 == 2:
print('That makes purple!')
elif color1 == 2 and color2 == 1:
print('That makes purple!')
elif color1 == 3 and color2 == 1:
print('That makes orange!')
elif color1 == 1 and color2 == 3:
print('That makes orange!')
elif color1 == 2 and color2 == 3:
print('That makes green!')
elif color1 == 3 and color2 == 2:
print('That makes green!')
else:
print('You did not enter a primary color!')
您的麻煩在於以下幾行:
color1 = bool(input('Enter your first primary color: \n'))
color2 = bool(input('Enter your second primary color: \n'))
執行此操作時,輸入的任何非空值都將導致True
。 您能夠獲得False
的唯一方法是在提示符后按return
並提交一個空字符串。 您想要處理用戶輸入內容的邏輯有些缺陷。 你可能想要:
if color1 == 'red'
在您刪除了多余的bool
調用之后,但這只是一個建議。
color1 = bool(input('Enter your first primary color: \n'))
如果我們簡化一下,我們得到
color1 = bool("red") #color1 becomes True
因此,現在您的比較正在評估False
等於True
完全忽略了您輸入的顏色。
嘗試類似
RED = 'red'
BLUE = 'blue'
color1 = input('Enter your first primary color: \n').lower()
color2 = input('Enter your second primary color: \n').lower()
if color1 == RED and color2 == BLUE:
print('That makes purple!')
您的代碼有一些大問題。 首先,您調用bool(input())
。 只要你提供一些輸入, color1
和/或color2
被設置為True。
因此,您在調用if True == False
...您的代碼不執行任何操作來檢查顏色的實際名稱。 相反,我建議使用普通的input()
接受` string 。
這是您編輯的代碼:
print('You will be mixing two primary colors to get a resulting color.')
print('Primary colors are blue, red and yellow \n')
color1 = input('Enter your first primary color: \n')
color2 = input('Enter your second primary color: \n')
if color1 == 'red' and color2 == 'blue':
print('That makes purple!')
elif color1 == 'blue' and color2 == 'red':
print('That makes purple!')
elif color1 == 'yellow' and color2 == 'red':
print('That makes orange!')
elif color1 == 'red' and color2 == 'yellow':
print('That makes orange!')
elif color1 == 'blue' and color2 == 'yellow':
print('That makes green!')
elif color1 == 'yellow' and color2 == 'blue':
print('That makes green!')
else:
print('You did not enter a primary color!')
你應該比較color1
和color2
為字符串不是布爾值:
color1 = input('Enter your first primary color: \n')
color2 = input('Enter your second primary color: \n')
if color1 == "red" and color2 == "blue":
print("That makes purple!")
問題似乎出在這些方面
red = False
blue = False
yellow = False
color1 = bool(input('Enter your first primary color: \n'))
color2 = bool(input('Enter your second primary color: \n'))
輸入顏色時,無論顏色是哪種,它都將被設置為false。 在計算機上正在發生(我將使用紅色和黃色作為示例)
color1 = bool(red)
由於您之前將紅色定義為False:
color1 = bool(False)
color1 = False
相似地
color2 = bool(yellow)
黃色也被定義為False
color2 = bool(False)
color2 = False
因此,當我們到達您的第一個if語句時
if color1 == red and color2 == blue:
電腦看到
if False==False and False == False:
評估為True
if True and True:
if True:
您使用部分input()可能會導致部分問題。 對於python2.x(我會假設您正在使用),input()會評估輸入的內容。 例如:
input("Enter Something")
>>>3
input("Enter Something Else")
>>>3.1
將分別返回整數值3和浮點值3.1。 計算機會查看您鍵入的內容,並盡力為您提供有意義的數據。 就你而言。
yellow = False
color1 = bool(input('Enter your first primary color: \n'))
>>>yellow
計算機將在程序的名稱空間中搜索黃色表達式的值,該表達式在該程序中為False。
它可以幫助您使用raw_input(),它不評估所鍵入的內容,而只是返回所鍵入內容的字符串值。 例如:
input()
>>>yellow
將評估為False,但
raw_input()
>>>yellow
將評估為字符串值“ yellow”
嘗試將顏色常量定義為字符串而不是布爾值,並將輸入也處理為字符串。 所以
yellow = "yellow"
而不是
yellow = False
我沒有為顏色使用變量...我只是測試了變量是否為=='color'
所以我的代碼是
color1=input('Enter primary color:')
color2=input('Enter primary color:')
if color1=='red' and color2=='blue':
print("When you mix red and blue, you get purple.")
elif color1=='red' and color2=='yellow':
print("When you mix red and yellow, you get orange.")
elif color1=='blue' and color2=='red':
print("When you mix blue and red, you get purple.")
elif color1=='blue' and color2=='yellow':
print("When you mix blue and yellow, you get green.")
elif color1=='yellow' and color2=='red':
print("When you mix yellow and red, you get orange.")
elif color1=='yellow' and color2=='blue':
print("When you mix yellow and blue, you get green.")
else:
print("You didn't input two primary colors.")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.