ValueError: could not convert string to float: in Python 3.10

2024/11/18 12:44:51

When someone writes a string or a letter, I want the code make them go back, and the code to print "must be a number and bigger than 0 and less than 100", but what actually happens is the code closing. Is there by any chance a fix for this?

import timedef main():num1 = input("your t76ely\n")num2 = input("your num2\n")school = input("your school\n")num1 = float(num1)num2 = float(num2)school = float(school)if num1 > 100:print("must be a number and bigger than 0 and less than 100")while True:time.sleep(1)main()elif num1 < 0:print("must be a number and bigger than 0 and less than 100")while True:time.sleep(1)main()if num2 > 100:print("must be a number and bigger than 0 and less than 100")while True:time.sleep(1)main()elif num2 < 0:print("must be a number and bigger than 0 and less than 100")while True:time.sleep(1)main()if school > 100:print("must be a number and bigger than 0 and less than 100")while True:time.sleep(1)main()elif school < 0:print("must be a number and bigger than 0 and less than 100")while True:time.sleep(1)main()else:r = (40 % 100 * float(num1)) + (30 % 100 * float(num2)) + (30 % 100 * float(school))r = r / 100print(r)print('\nnum1 :', {num1}, '\nnum2 :', {num2}, "\nschool :", {school})while True:main()if input("\nWant To continue ? Y/N\n").upper().strip() != "Y":break

It's showing me this when I try to type for example my name "trais" or a letter or a "string"

Traceback (most recent call last):File "D:\Python Projects\your mark\mark calculator.py", line 51, in <module>main()File "D:\Python Projects\your mark\mark calculator.py", line 9, in mainnum1 = float(num1)
ValueError: could not convert string to float: 'trais'
Answer

You can use PyInputPlus. If you are okay with installing library.

float() will convert numeric string to float type. Not any other string which is not numerical.

Basic snippet for keep on going until input is correct (without any prompt)

def trial():try:a=float(input("Enter 0 to 100"))print(a,type(a))except:print("Enter number between 0 to 100")trial()
https://en.xdnf.cn/q/120078.html

Related Q&A

How do I access Class fields in Python Graph-Tool property maps?

Im trying to draw a graph with a class as a vertex property. How do I draw the graph with the vertex_text set to the name field of the classes they contain?from graph_tool.all import *class Node(objec…

How to iterate through each line of a text file and get the sentiment of those lines using python?

Currently, Im working on Sentiment Analysis part. For this I have preferred to use Standford Core NLP library using python. Im able to get the sentiment for each sentence using the following code : fro…

RECURSIVE function that will sum digits of input

Trying to write a piece of code that will sum the digits of a number. Also I should add that I want the program to keep summing the digits until the sum is only 1 digit. For example, if you start with …

Make sure matrix row took from text file are same length(python3) [duplicate]

This question already has answers here:Making sure length of matrix row is all the same (python3)(3 answers)Closed 10 years ago.so I have this code to input a matrix from a text file:import ospath = in…

how to randomize order of questions in a quiz in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

How transform days to hours, minutes and seconds in Python

I have value 1 day, 14:44:00 which I would like transform into this: 38:44:00. Ive tried the following code: myTime = ((myTime.days*24+myTime.hours), myTime.minutes, myTime.seconds) But it doesnt work.…

Brute Force in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

Choosing only non-zeros from a long list of numbers in text file

I have a text file with a long list of numbers. I would like to choose only the non-zeros and make another text file. This is a portion of the input file:0.00000E+00 0.00000E+00 0.00000E+00 0.00000…

Why can I not plot using Python on repl.it

For practical reasons, I want to test a small piece of Pyton code on repl.it (webbased, so I do not need to install Python).The codeimport numpy as np import matplotlib.pyplot as plttime = np.array([0,…

Pull Data from web link to Dataframe [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…