2

It says that a float is required. Can anyone help me fix this code:

from time import sleep
print("Welcome to this game of random.") ; sleep(1.0)
print("Type in your name:") ; sleep(0.5)
playerName = raw_input()
print("Welcome " + playerName + " Type how fast do you want the text to go in this format: 1.0") ; sleep(1.0)
speed = raw_input()
print("You choosed: " + speed) ; sleep(speed)

Is there an error that needs to be fixed?

Sergio Ley
  • 85
  • 1
  • 14

2 Answers2

0

speed is a string and you are doing sleep(speed)

sleep requires a float or int so you need to do a sleep(float(speed)). This assumes the input will be a number and therefore won't fail when converting to a float

You should be validating the input of speed to ensure it is a proper format using try/except

MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
0

Because sleep takes a number (int or float) as parameter, you need to cast the value returned by raw_input when it is assigned to speed. I think you are using Python 2, so raw_input which equivalent in Python 3 is input, will return a string:

from time import sleep
print("Welcome to this game of random.") ; sleep(1.0)
print("Type in your name:") ; sleep(0.5)
playerName = raw_input()
print("Welcome " + playerName + " Type how fast do you want the text to go in this format: 1.0") ; sleep(1.0)
## LOOK AT THE FOLLOWING LINE
speed = float(raw_input())
print("You choosed: " + str(speed)) ; sleep(speed)

Also, do not forget to convert speed to str since you cannot concatenate a str and float.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228