[Tutor] Try Exclude and operators

Mats Wichmann mats at wichmann.us
Mon Dec 18 18:11:57 EST 2023


On 12/18/23 12:18, Matt Athome via Tutor wrote:
> Python version
> Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
> 
> Purpose
> Give test score for input between 0.0 to 1.0
> Use the try and exclude to throw up a message for invalid user input.
> 
> code gives the following error.
>   score >= 0 and <= 1.0
>                          ^^
> SyntaxError: invalid syntax
> 
> # Score between 0.0 and 1.0
> score = float(input("Enter Score: "))
> try:
>      score >= 0 and <= 1.0
> except:
>      print("Enter a score between 0.0 and 1.0")


Also consider other kinds of invalid input:

 >>> score = float(input("Enter Score: "))
Enter Score: A+
Traceback (most recent call last):
   File "<input>", line 1, in <module>
     score = float(input("Enter Score: "))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: 'A+'
 >>>





More information about the Tutor mailing list