2017-02-24 78 views
0

要從變量「glory」中取1,變量需要是整數。當它變成一個整數時,我只會收到:「無法分配函數調用」。無法爲函數調用或不支持操作數類型爲 - =:'str'和'int'

import sys 

glory = input("Glory charge: ") 
glory_prev = glory 
print(glory_prev) 
pouch = 28 
run_num = 0 
nat_price = input("Nat price: ") 

while True: 
    run = input("You've done " + (str(run_num)) + " runs: ") 
    if run == "a": 
     (int(glory) -= 1 
     pouch -= 1 
     if glory == 0: 
      print("New glory needed") 
      glory = glory_prev 
     if pouch == 0: 
      print("Repair pouch") 
      pouch = 28 

    elif run == "q": 
     sys.exit 
    else: 
     continue 

請幫忙,謝謝。

+0

你所期望的' (int(glory) - = 1'做什麼? –

+0

從榮耀中取出1 – FynFTW

+0

int(glory)用參數glory調用函數int,它可能會返回一個整數然後你試圖做一些像'10 - = 1',這沒有任何意義 –

回答

1

你必須要考慮的唯一的事情是榮耀INT之前移除支架和更改榮耀語法

glory = int(glory) - 1 

這樣的代碼會是這樣

import sys 

glory = input("Glory charge: ") 
glory_prev = glory 
print(glory_prev) 
pouch = 28 
run_num = 0 
nat_price = input("Nat price: ") 

while True: 
    run = input("You've done " + (str(run_num)) + " runs: ") 
    if run == "a": 
     glory = glory - 1 
     pouch -= 1 
     if glory == 0: 
      print("New glory needed") 
      glory = glory_prev 
     if pouch == 0: 
      print("Repair pouch") 
      pouch = 28 

    elif run == "q": 
     sys.exit 
    else: 
     continue 
+0

不能分配函數調用,因爲你只是以不正確的方式給函數輸入。每當你在=運算符的左邊寫一個函數,並賦值給它,它會給出這個錯誤,例如:a = 3; int(a)= 4; –

+1

現在有效。非常感謝:) – FynFTW

+0

雖然這是正確的,但我覺得在每次迭代中都會把榮耀投射到一個整數上感覺很奇怪。爲什麼不在開始時只做一次? – bouletta

相關問題