今天繼續分享python 做題
我今天做到第4章的14題,應該說從6月7日到今天上午。我一直在做著道題。隨機產生3個數字,然後我們猜全中給10000,數字猜中但順序不對給3000.猜對乙個數給1000
該程式原為隨機出2位數,然後猜2位數。習題改成了3位數。
import random
lottery = random.randint(0, 999)
print(lottery)
guess = eval(input("enter your lottery pick (three digits): "))
lotteryd1 = lottery // 100
# print(lotteryd1)
lotteryds = lottery % 100 此處上一次練習時並沒有注意到應該取100%,這個算上一次的乙個錯誤
# print(lotteryds)
lotteryd2 = lotteryds // 10
# print(lotteryd2)
lotteryd3 = lottery % 10
# print(lotteryd3)
guessd1 = guess // 100
# print(guessd1)
guessds = guess % 100
# print(guessds)
guessd2 = guessds // 10
# print(guessd2)
guessd3 = guessds % 10
# print(guessd3)
#接著下面這個地方是我糾結了將近一天的地方。 首先我先從好理解的出乙個數這方面考慮,編出了
「elif guessd1 or guessd2 or guessd3 == lotteryd1 or lotteryd2 or lotteryd3:」這是我第2次刷題時的新想法,不錯。
if guess == lottery:
print("lucky is 10000 ")
elif (guessd2 == lotteryd1 and guessd1 == lotteryd2 and guessd3 == lotteryd3):
print("come guest good
3000")
elif (guessd2 == lotteryd1 and guessd3 == lotteryd2 and guessd1 == lotteryd3):
print("come guest good
3000")
elif (guessd1 == lotteryd1 and guessd3 == lotteryd2 and guessd2 == lotteryd3):
print("come guest good
3000")
elif (guessd3 == lotteryd1 and guessd2 == lotteryd2 and guessd1 == lotteryd3):
print("come guest good
3000")
elif (guessd3 == lotteryd1 and guessd1 == lotteryd2 and guessd2 == lotteryd3):
print("come guest good
3000")
elif guessd1 or guessd2 or guessd3 == lotteryd1 or lotteryd2 or lotteryd3:
print("more gogo is 1000")
接著面對數字對但順序不對。我在此想了很長時間。然後相出了一下的公式。但是結果往往出現了2種情況
764 #此處是我列印出了隨機結果以便測試用
enter your lottery pick (three digits): 467 #提示使用者輸入
more gogo is 1000 #獎金提示。
大家看到了數字全對,順序不對應該是3000.但是這裡不論順序不對還是只出乙個數,他都執行的是「or」。我首先懷疑的是我寫的and and 這樣的公式不對。為了測試我在程式的下方有寫了以下幾句
# 測試用
# print(guessd2 == lotteryd1 and guessd1 == lotteryd2 and guessd3 == lotteryd3)
# print(guessd1 == lotteryd1 and guessd3 == lotteryd2 and guessd2 == lotteryd3)
# print(guessd3 == lotteryd1 and guessd2 == lotteryd2 and guessd1 == lotteryd3)
# print(guessd2 == lotteryd1 and guessd3 == lotteryd2 and guessd1 == lotteryd3)
經過這些語句的測試,我發現我的設想並沒有錯。我寫的也沒有錯。那是什麼道理呢。直到剛才。我才想到是不是順序。python是從上往下執行。那麼我把
# elif guessd1 or guessd2 or guessd3 == lotteryd1
or lotteryd2 or lotteryd3:
# print("more gogo is 1000")
放在了首位。後面則是判定數字不是順序的and 造成計算機只執行到or欄位後,就停止了執行。
所以我做了更改,將順序改為文章開頭你們看到的樣子。
非常感謝大家的**
希望大家多提意見。謝謝
產生8位數的隨機數
1 隨機數的基數為大寫字母26個,和數字0 9 namespace test random rd new random for inti 0 i 8 i console.writeline result console.readline 2.若8位數都要產生不相同的字母和數字,則 使用如下 如果你想...
產生8位數的隨機數
1 隨機數的基數為大寫字母26個,和數字0 9 view code namespace test random rd new random for inti 0 i 8 i console.writeline result console.readline 2.若8位數都要產生不相同的字母和數字,則...
Android 顏色,6位和8位數顏色的區別和應用
我們在android開發時常用的顏色值為6位或3位如 android background cccccc 或android background ccc 那如果顏色值是8位的是表示什麼意思,會是什麼效果呢?如下 android background 88cccccc 三位和六位的是rgb色值rrgg...