Python Forum
ValueError: could not convert string to float:
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: could not convert string to float:
#1
i tried to write a small program using tkinter and other small libraries, but i ran into a problem converting the string into a float in line 13, could anyone help? there might be some other inconsistencies in the program as well (because i didnt really finish it yet), if you can also help me with those, that would be awesome, thank you!

import tkinter as tk
import random
import time
window = tk.Tk()

def startthing():
    random_float = random.random() + 1
    limit = random.randint(0,10)
    x = 0
    bet = bettingentry.get()
    value = int(rising["text"])
    while x!=limit:
        betting["text"] = (float(bet) * random_float)
        x = x + 1
        time.sleep(1)
    


rising = tk.Label(text = 1)
rising.pack()

betting = tk.Label(text = 0)
betting.pack()

bettingentry = tk.Entry()
bettingentry.pack()

buttonentry = tk.Button(
    text = "bet the money /",
    command = startthing()
)
buttonentry.pack()
buttonentry.bind("<Button-1>", startthing())

window.geometry('500x500')
window.mainloop()
Error:
File "/Users/nope/Desktop/testing_app.py", line 13, in startthing betting["text"] = (float(bet) * random_float) ValueError: could not convert string to float:
Reply
#2
The exception message tells you that what you're passing to float is the empty string (else you'd see the string you're passing). I don't know Tkinter, so it's not obvious to me what's wrong, other than the field just being empty of course.
Reply
#3
Looks like the bet is a string ,i.e., like a word or something, which cannot be converted into a floating point decimal, which only consists. And there is an error in your line 10 -
bet = bettingentry.get()
Bettingentry isn't defined anywhere. And, I don't think so its a module
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
bettingentry is defined on line 25 - remember LEGB.

Having another quick skim through the code: is line 30 the problem? Did you intend to call the function there, passing Its return value as command, or should you be passing the function itself? It's an educated guess - I assume that the function passed as command is called when the button is pressed. Drop the parens if you did mean to pass the function. I suspect the same is true for line 33.

The docs at effbot.org seem to confirm my suspicion (note that bind is inherited from the Widget class, so also see its docs).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 0 5 14 minutes ago
Last Post: sirocawa
  convert string to float in list jacklee26 6 1,890 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 5,909 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,312 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,507 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,835 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,368 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,423 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Convert a string to a function mikepy 8 2,490 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,351 Apr-12-2022, 08:31 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020