0
       # -*- coding: cp1250 -*-
print ('euklides alpha 1.0')
a = raw_input('podaj liczbę A  : ')
b = raw_input('podaj liczbę B  : ')

a = float('a')
b = float('b')

if 'a'=='b':
    print 'a'
elif 'a' > 'b':
    while 'a' > 'b':
        print('a'-'b')
        if 'a'=='b': break
        if 'a' > 'b': continue
elif 'b' > 'a':
    while 'b' > 'a':
        print('b'-'a')
        if 'b'=='a': break
        if 'b' > 'a': continue

So, this is a code, which I made few hours ago. Now I get a ValueError: could not convert string to float: a, and I have no idea why. Can you explain it to me? I'm a beginner.

2
  • 1
    Why are you making everything strings? 'a' is the character a - a (no quotes) is the variable name a... So float('a') is wrong - you can't make a float from the letter a - it should be float(a) where a will be the content of what's been entered... Commented Dec 2, 2013 at 21:48
  • ohhhhhhhh yes, now I see it, than you for support :D Commented Dec 2, 2013 at 21:56

2 Answers 2

2

the float function can take a string but it must contain a possibly signed decimal or floating point number. You want to make the variable a a float not the char 'a'.

You don't need all the ' around your variable names. When you put quotes around them 'b' you are making them a string.

On another note once you reach on of those while statements there's nothing that will get you out of there.

a = float(a)


if a == b: # you need to get rid of all the ' unless you are talking about chars 



   # -*- coding: cp1250 -*-
print ('euklides alpha 1.0')
a = raw_input('podaj liczbę A  : ')
b = raw_input('podaj liczbę B  : ')

a = float('a')
b = float('b')

if a==b:
    print a
elif a > b:
    while a > b: # nothing will get you out of the while loop
        print(a-b)
        if a == b:
            break
        if a > b: # no need for this if, the while loop will do that check for you
            continue
elif b > a:
    while b > a: # nothing will get you out of the while loop
        print(b-a)
        if b==a:
            break
        if b > a: # no need for this if, the while loop will do that check for you
            continue
Sign up to request clarification or add additional context in comments.

Comments

1

You're enclosing all your variables in single quotes, so you're comparing the string "a" with the string "b" rather than the floating point number contained in variable a with the same in variable b

Also worth pointing out:

This is your original code, to request two floating point numbers from the user

a = raw_input('podaj liczbę A  : ')
b = raw_input('podaj liczbę B  : ')
a = float(a)
b = float(b)

If you're in Python 2.x, you can instead use

I was corrected by abarnert in comments below. Just leaving it for edification:

I wouldn't suggest using input instead of raw_input and float. The latter guarantees that you get floats; the former could get, say, the result of calling __import__('os').system('rm -rf /'), which is probably something you don't want

a = input('podaj liczbę A  : ')
b = input('podaj liczbę A  : ')

If you're not, you may want to include a try/catch block that will force the variable to be of the type you require

a_verifying = True
while a_verifying:
  a = input('podaj liczbę A  : ')
  try:
    a = float(a)
    a_verifying = False
  except ValueError as e:
    a = input('podaj liczbę A  : ')

b_verifying = True
while b_verifying:
  b = input('podaj liczbę B  : ')
  try:
    b = float(b)
    b_verifying = False
  except ValueError as e:
    b = input('podaj liczbę B  : ')

2 Comments

I wouldn't suggest using input instead of raw_input and float. The latter guarantees that you get floats; the former could get, say, the result of calling __import__('os').system('rm -rf /'), which is probably something you don't want.
abarnert: I defer to your opinion. I only started using Python recently, and when I started I learned "input() for numbers, raw_input() for strings." Now I'm in 3.x and don't care about such distinctions :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.