2

I'm a newbie on programming and such. I bought a book about python and learn python from it. But right now, I'm confused about this script on python 2.7 seems like it is not error but when i'm running it on cmd it shows error like this :

Traceback (most recent call last):
  File "E:\bahasa mesin\eksepsi.py", line 18, in <module>
    main()

File "E:\bahasa mesin\eksepsi.py", line 6, in main

 a = float(raw_input("masukkan a: 13.0"))

ValueError: could not convert string to float:

enter image description here

Sorry for my bad english and attitude

I hope someone can help me to fix the script.

2
  • 1
    You're not actually passing anything to the input, so raw_input returns an empty string, which float rejects. I think you are meant to type in 13.0 not make it part of the prompt. Commented Jun 12, 2017 at 19:10
  • oh i see.. thanks @juanpa.arrivillaga Commented Jun 13, 2017 at 2:38

2 Answers 2

1

It looks like you're giving raw_input no input. So converting no input to a floating point number fails. It looks like you want to pass in 13.0 and 4.0. Try this:

def main():
    print("Your Text Here")
    a = float(raw_input("Enter a: ")) #if using python 3, use input()
    b = float(raw_input("Enter b: "))
    try:
        hasil = a/b
    except ZeroDivisionError:
        print("ERROR")
    else:
        print("a: ", a) ##print() ends it with a new line so \n is redundant 
        print("b: ", b)              #unless you want an extra line.
        print("a/b = ", hasil)
if __name__ == "__main__":
    main()

Then when you run it, when prompted for a type in what you want; i.e. 13.0 or any other value and the operations will work.

Sign up to request clarification or add additional context in comments.

Comments

0

Strings are characters or symbols. For example to put it in simple terms your name 'aku firman' would be a string. NOTICE the '' quotes.

But your age can be an integer. Say 20 is your age.

n = 20 meaning n is an integer.

But anything given between quotes can be considered as a string.

i.e n = '20' or n = "20" (Even double quotes can be used) Here n is a string. Read more about strings here Strings explained

Also raw_input presents a prompt to the user gets input from the user and returns the data input by the user in a string. See the docs for raw_input

a = float(raw_input("masukkan a: 13.0"))

Here you don't input the value 13.0 to float() function rather you wait for the user to enter something with the prompt displayed on your cmd as masukkan a: 13.0

So if you want to get input from user and convert it to float.

For example try this in python interpreter:

To avoid confusion i'll seperate what you have to type in interpreter from it's corresponding output in interpreter since you are a newbie:

x=float(raw_input("masukkan a: 13.0\n"))                 
y=float(raw_input("masukkan b: 4.0\n"))
x/y

Along with output:

>>>x=float(raw_input("masukkan a: 13.0\n"))
masukkan a: 13.0
13                 
>>>y=float(raw_input("masukkan b: 4.0\n"))
masukkan b: 4.0
4
>>>x/y
3.25

But this

>>>x=float(raw_input("masukkan a: \n"))
masukkan a: 
13                 
>>>y=float(raw_input("masukkan b:\n"))
masukkan b:
4
>>>x/y
3.25

See how the prompt changed. You get the idea right?

Also the \n makes you to enter input date in a newline.

3 Comments

Why do you still have the 13.0 and 4.0 in the raw_input string? Isn't that for the user to enter?
Kept that to help him understand it more clearly. But he would've got confused thanks for pointing it out.
Try learning more and more. Everybody is there to help. cheer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.