New OOTS products from CafePress
New OOTS t-shirts, ornaments, mugs, bags, and more
Results 1 to 6 of 6
  1. - Top - End - #1
    Barbarian in the Playground
    Join Date
    Aug 2006
    Gender
    Male

    Default Why can't I get even a basic thing to work in Python?



    I'm trying to learn Python using Python 3.8.5 and just trying the most basic program I can find. I'm pretty much a novice to programming.

    I tried this:

    temp = eval(input('Enter a temperature in Celsius: '))
    print('In Fahrenheit, that is', 9/5*temp+32)

    But all I got in response was

    SyntaxError: multiple statements found while compiling a single statement

    What am I doing wrong? Please help.

  2. - Top - End - #2
    Dwarf in the Playground
     
    Imp

    Join Date
    Jan 2019

    Default Re: Why can't I get even a basic thing to work in Python?

    what is the "eval" for?

    eval means evaluate, and is used to treat a string as code. You are not wanting that. You are already writing code.

    You could make it work by putting double quotes around the whole input statement, to turn your code into a string, and then use eval to turn it back into code, but far simpler is just write the code.

    temp =input('Enter temperature')

    The problem here is that 'input' gives you a string (words, basically), when you want a number. The float function turns a string into a number, so you want

    temp = float(input('...'))

    Edit: Oh, I see; the eval was evaluating the string, which cast it to a float. A bit weird, but that makes sense. Probably the later posters actually had the problem.
    Last edited by Fat Rooster; 2020-07-27 at 06:17 PM.

  3. - Top - End - #3
    Troll in the Playground
     
    DwarfClericGuy

    Join Date
    Jun 2007

    Default Re: Why can't I get even a basic thing to work in Python?

    Are you copying and pasting into a shell?

    Cause that can cause ... issues.
    May you get EXACTLY what you wish for.

  4. - Top - End - #4
    Barbarian in the Playground
    Join Date
    Aug 2006
    Gender
    Male

    Default Re: Why can't I get even a basic thing to work in Python?

    Quote Originally Posted by sihnfahl View Post
    Are you copying and pasting into a shell?

    Cause that can cause ... issues.
    Oh. I figured out what I was doing wrong. I needed to do this in a new window instead of the main window and save it first?

  5. - Top - End - #5
    Troll in the Playground
     
    DwarfClericGuy

    Join Date
    Jun 2007

    Default Re: Why can't I get even a basic thing to work in Python?

    Quote Originally Posted by Arcane_Secrets View Post
    Oh. I figured out what I was doing wrong. I needed to do this in a new window instead of the main window and save it first?
    Well, it depends on the environment.

    The basic version would be shell - save your code as, say, tempconvert.py, then shell with 'python tempconvert.py'

    If you're using an online Python parser, you can just run as is.

    Or in an IDE like Spyder.
    May you get EXACTLY what you wish for.

  6. - Top - End - #6
    Barbarian in the Playground
     
    PaladinGuy

    Join Date
    Sep 2016

    Default Re: Why can't I get even a basic thing to work in Python?

    Cheap pun,
    Because Basic things work in Basic, for Python you need Python
    ___
    I thought it might have been a combination of Fat Rooster and Shinfahl's points.
    In fact looking online it is just Shinfahl's, there's nothing wrong with the program, there was something wrong with how you tried to put it in.

    Actually looking it online, it seems some interactive prompts, just throw a wobbly like that when they get two lines pasted in that way.
    [That main window] is designed to give you back an answer for each line, and then get the next line.
    You've given it lots of lines, it could guess that you want it to do a line then the next. but what if one of your lines is wrong (inevitable in interactive prompt), do you want to go back to the start, just stop at the broken line, what do you want to do about input? Lets just not get involved.
    When you load from a file, it has confidence you know what you are doing and just gets on with it. If it turns out to be wrong to trust you it just stops completely.

    Other interactive prompts use the same input for line input and when the program inputs (e.g. when I run python from the terminal)
    This has issues (which is why the fancy interactive prompt doesn't get involved).
    If you tried inputing it line by line, you might be able to replicate them.
    Spoiler
    Show

    Did it come back with 'Enter a temperature in Celsius: ' after you entered the first line and before you entered the second?
    If so I think it's a combination of Fat Rooster and Shinfahl's points.

    The main window is dealing with each line as you type it (you get interactive python windows, it is a guess that we are looking at one here)
    You tell it that temp=eval(input("Message))
    The interactive window begins this. It prints Message and waits for your input
    You give your input (thinking you are putting the next line) print("In fahrenheit that is", temp)
    It dutifully tries to evaluate what to do, that is print("in fahrenhiet that is ",temp)
    But because it's still following the eval, it hasn't put anything in temp, so something funny happens deppending on the previous vallue

    If I try copying and pasting you code into the interactive console, I gt the following:

    >>> temp=5
    >>> temp = eval(input('Enter a temperature in Celsius: '))
    Enter a temperature in Celsius: print('In Fahrenheit, that is', 9/5*temp+32)
    In Fahrenheit, that is 41.0
    [NB the prompt appears in the middle, and I get 9/5*5+32]

    >>> temp=None
    >>> temp = eval(input('Enter a temperature in Celsius: '))
    Enter a temperature in Celsius: print('In Fahrenheit, that is', 9/5*temp+32)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<string>", line 1, in <module>
    TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

    I can't get your error, but if ever temp was ever a string that caused that error, copying and pasting the code would repeatedly cause that error while not doing so when you load it from a file, before it starts running so it doesn't get confused.

    Last edited by jayem; 2020-07-27 at 04:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •