0

i'm trying to print the operation however the first statement is working but second statement has error.

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)
terry
  • 43
  • 7
  • 1
    print ("Total number of spent = {0} ".format(totalSpent)) or "Total cycle request = {0}".format(totalCycle) – Ari Gold Nov 15 '16 at 17:05
  • I think it's because you're trying to concatenate a float with a string. Try `print (str(totalCycle) + " * " + str(reqSecond))` – LismUK Nov 15 '16 at 17:08

2 Answers2

1

The problem here is that the variable totalCycle is of type float. Python does not know what it means to do + between a type float and string (because " * " is a string).

To do it the way you showed you have to convert totalCycle to string first, like this:

print (str(totalCycle) + " * " + reqSecond)
Rok Povsic
  • 4,626
  • 5
  • 37
  • 53
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'.
Ari Gold
  • 1,528
  • 11
  • 18