2016-11-15 5 views
0

Ich versuche die Operation zu drucken, aber die erste Anweisung funktioniert, aber die zweite Anweisung hat einen Fehler.nicht unterstützter Operandentyp "float" und "str" ​​

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

print ("Gesamtzahl abgebrannter = {0}". format (totalSpent)) oder "Gesamtzyklus-Anfrage = {0}". format (totalCycle) –

+0

Ich denke, das liegt daran, dass Sie versuchen, einen Float mit einer Zeichenkette zu verketten. Probieren Sie "print (str (totalCycle) +" * "+ str (reqSecond))" aus – LismUK

Antwort

1

das Problem hierbei ist, dass die Variable totalCycle vom Typ ist float. Python weiß nicht, was es bedeutet, + zwischen einem Typfloat und Zeichenfolge zu tun (weil " * " ein String).

es damit zu tun, Sie zeigte man totalCycle auf Zeichenfolge zu konvertieren haben zuerst, wie folgt aus:

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'. 
Verwandte Themen