Ⅰ python菜鳥問題,如下圖,為何答案是永不停止呢當「guess=5.0"時,while 循環體內的 abs(guess**2-x)不
測試一下就知道,python的浮點運算精度上有詭異的「bug」,加的是0.1,但很有可能print出來變成0.09999999...,導致剛好偏差了一點
就算你說的當「guess=5.0"時,if不再滿足,但不滿足後,guess也不會再加,那麼guess就永遠小於25,while不會停止
改成下面就可以停止
whileguess<=x:
ifabs(guess**2-x)<epsilon:
break
guess+=step
Ⅱ python初學者,為什麼if guess後面不能跟字元串,怎麼才能跟呢,謝謝,我這么寫運行後輸入
有兩個問題:
一、if 後面要加冒號
二、guess=int(temp)語句是錯的,因為你要判斷的是字元串,不能化為整數
修改如的代碼如下:
print("放馬過來")
temp=input("百家姓第一個是啥")
if temp=="周":
print("正確")
else:
printf("不對")
print("游戲結束")
Ⅲ guess = int(temp),在python是什麼意思
int()是強制類型轉換的意思,操作方法如下:
1、首先在python編輯器中,直接輸入【】int()】,會得到0。
Ⅳ python3.7版本guess = int(temp) ValueError: invalid literal for int() with base 10: '' 怎麼辦啊
temp取值應該是空字元串才會報這個錯,你看下temp的取值
Ⅳ Python中1、為什麼guess要賦值為0
開始guess不是0也可以啊。只要值不在1到10之間就可以了。假如你用了1,隨機數出來也是1,那麼就不用猜了。while循環都不會執行。times放在前面後面都可以。兩個條件要同時滿足才會進入到while中。
Ⅵ 初學python,被作業難到了,做一個猜數游戲,給十次機會,寫完運行不出來,求大佬看看
import random
target=random.randint(1,1000)
count=0
while True:
try:
guess=eval(input("猜猜這個數是什麼,一共有10次機會哦"))
except:
continue
print("請輸入一個整數")
if guess<target:
print("猜小了")
count=+1
elif guess>target:
print("猜大了")
count=+1
elif count==10:
print("機會用完了,歡迎下次再來!")
break
else:
print("猜對了,正確答案為",target,"/n","一共猜了{}次".format(count))
break
Ⅶ 用python寫猜數字小游戲
核心代碼給你,具體的功能還需要自己完善。
importtime,random
classGuessNum:
def__init__(self):
self._num=''
self.input_num=[]
self.count=1#猜對所用次數
self.sec=0#猜對所用時間
self._generate_num()
def_generate_num(self):#產生不重復的四個數字
seq_zton=list(range(10))
foriinrange(0,4):
a=str(random.choice(seq_zton))#選出一個數字
self._num+=a
seq_zton.remove(int(a))#注意a的類型
self.sec=time.clock()#開始計時
defcheck_answer(self):
returnself._num
defcheck_input(self):
num_pos,num_value=0,0#位置對和數值對的分別的個數
tmp=input("Pleaseinputthenumberyouguess(Norepetition),or'c'tochecktheanswer:")
iftmp=='c':
print(self.check_answer())
tof=self.check_input()
returntof
elifnottmp.isalnumornotlen(tmp)==4:
print("Wrongformat!")
tof=self.check_input()#需要優化
returntof
self.input_num=list(tmp)
lst_temp=list(self._num)
ifself.input_num==lst_temp:#猜對
self.prt_vic()
returnTrue
foriinlst_temp:
ifiinself.input_num:
iflst_temp.index(i)==self.input_num.index(i):#位置也相同
num_pos+=1
num_value+=1
else:
num_value+=1
self.prt_state(num_pos,num_value)
self.count+=1
returnFalse
defprt_state(self,num_pos,num_value):
print("You'vegot%%dnumberswiththerightvalueonly"%(num_pos,num_value))
defprt_vic(self):
t=time.clock()
self.sec=t-self.sec
print("Congratulations!!")
print("%dtimesand%."%(self.count,self.sec))
gn=GuessNum()
whileTrue:
ss=gn.check_input()
ifss:
b=input("Continue?y/n:")
ifb=='n':
break
else:
gn=GuessNum()
continue
Ⅷ python 里 guess = int(temp報錯
#
-*-
coding:
cp936
-*-temp=input("猜猜數字")guess=int(temp)if
guess==8:
print"你是蛔蟲嗎"
print"哼,中了"else:
print"錯了,"
print"游戲結束!"input()
和int()都是內建函數,帶括弧。
Ⅸ python初學者問題,嘗試後無法解決,請大神解釋
"""
__________運行環境python3.5,結果正常_______
"""
temp=input('不妨猜一下,我在想什麼數字?')
guess=int(temp)
ifguess==8:
print('哇,這么厲害!')
print('猜中也沒有獎!')
else:
print('猜錯啦!我想的是8!')
print('游戲結束!')
Ⅹ Python猜數字游戲為什麼顯示錯誤次數
你的猜數字游戲的Python程序中,記錄所猜次數的變數guessesTaken,僅賦了一個初始值0,程序中並沒改變其值,所以錯誤次數一直是0,你只需要在for-i循環中,guess=int(guess)下面, if guess<number :上面,加一句guessesTaken=guessesTaken+1 就會是正確的用了幾次機會 猜中數字的數值了.
注意 這里用了幾次機會猜中數字的數值比猜錯的次數多一,所以如果你要列印猜錯次數,只需要列印guessesTaken-1 就行了.