2016-11-15 77 views
0

我试图打印但是第一条语句是工作的运作,但第二条语句有错误。不支持的操作数类型“浮动”和“海峡”

conSwitch = raw_input("Enter cycle of context switch: ") 
cycleOpr = raw_input("Enter cycle operation: ") 

totalCycle = float(conSwitch) + float(cycleOpr) + float(conSwitch) 

print "Context =", conSwitch 
print "Cycle operation =", cycleOpr 
print "Context switch back =", conSwitch 
print("\n") 

print (conSwitch + " + " + cycleOpr + " + " + conSwitch) 
print ("Total number of cycle is: " + format(totalCycle)) 
print("===================================================") 

reqSecond = raw_input("Enter cycle request second:") 

print "Total cycle request =", totalCycle 
print "Cycle request per second =", reqSecond 
print("\n") 

totalSpent = float(totalCycle) * float(reqSecond) 
print (totalCycle + " * " + reqSecond) 
print ("Total number of spent = " + format(totalSpent)) 

=========================================== ===================

First statement 
Work===>> print (conSwitch + " + " + cycleOpr + " + " + conSwitch) 

Second statement 
Error===>> print (totalCycle + " * " + reqSecond) 
+1

打印( “总数花= {0}”。格式(totalSpent))或“总周期请求= {0}”。格式(totalCycle) –

+0

我想这是因为你试图连接具有一个字符串浮动。尝试'打印(STR(totalCycle)+ “*” + STR(reqSecond))' – LismUK

回答

1

的这里的问题是,可变totalCycle类型float的。 Python不知道这意味着什么做+一种浮动之间(因为" * ")。

要做到这一点,你表现出你要totalCycle转换为字符串第一,这样的方式:

print (str(totalCycle) + " * " + reqSecond) 
0

FORMAT Syntax

"First, thou shalt count to {0}" # References first positional argument 
"Bring me a {}"     # Implicitly references the first positional argument 
"From {} to {}"     # Same as "From {0} to {1}" 
"My quest is {name}"    # References keyword argument 'name' 
"Weight in tons {0.weight}"  # 'weight' attribute of first positional arg 
"Units destroyed: {players[0]}" # First element of keyword argument 'players'. 
相关问题