Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7006255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:27:16+00:00 2026-05-27T21:27:16+00:00

I tried flushing stdout with sys.stdout.flush() but it’s still not working. It works fine

  • 0

I tried flushing stdout with sys.stdout.flush() but it’s still not working. It works fine until the user only enters C or F which breaks the code. Suffix is the first function called
so I make sure that if the user only enters one character then an error is returned. But once the error is returned the user is no longer able to type ‘quit’ or ‘q’.

#!/usr/local/bin/python
#Converts between Celsius and Fahrenheit
import random, sys

def check_version():
    """
    Make sure user is using Python 3k
    """
    if(sys.version_info[0] != 3):
        print("Stop peddling with your feet Fred!")
        print("Only Py3k supported")
        sys.exit()
    else:
        pass

def random_insult():
    """
    Returns a list of random insults with the sole purpose of insulting the user
    """
    insults = ["Kel", "stimpy", "knucklehead"]
    return insults[random.randrange(3)]

def suffix(temp):
    """
    Accepts the input temperature value which should be a string suffixed by C(c) or F(f)
    Returns the last element of the input string(C or F)
    """
    if(len(temp) >= 2):
        return temp[len(temp)-1]
    else:
        temperature("Input String TOO Small")

def temp_value(temp):
     """
     Accepts the input temperature value which should be a string suffixed by C(c) or F(f)
     Returns the actual temperature value
     """
     if(len(temp) >= 2):
         return temp[0:len(temp)-1]
     else:
         temperature("Input String TOO Small")

def cel_to_far(temp):
    """
    Accepts the input temperature value as Celsius and returns it in Fahrenheit
    """
    try:
        return ((temp * (9/5.0)) + 32)
    except TypeError:
        return "Has to be a number"

def far_to_cel(temp):
    """
    Accepts the input temperature value as Fahrenheit and returns it in Celsius
    """
    try:
        return ((temp - 32) * (5/9.0))
    except TypeError:
        return "Has to be a number"

def temperature(error=None):
    """
    Loops until the user enters quit or q. Allows the user to enter the temperature suffixed by either C(c) or F(f). 
    If suffixed with C then the temperature is taken as Celsius and converted to Fahrenheit.
    If suffixed with F then the temperature is taken as Fahrenheit and converted to Celsius.
    If the user enters anything else be sure to belittle him/her.
    """
    prompt1 = "Enter value suffixed by C or F *\n"
    prompt2 = "Type 'quit' or 'q' to quit     *\n"
    error1 = "What in the world are you doing "+ random_insult() + "?\n"
    error2 = "Did you forget to add C or F to the end of the value?\n"
    example = "Here's an example of input: 30F\n"
    stars = ("*" * 32) + "\n"
    temp = None
    if(error != None):
        print(error)
        print(example)
        temperature()        
    else:
        while(True):
            sys.stdout.flush()
            try:
                temp = input("\n"+ stars + prompt1 + prompt2 + stars + ">>") 
                if( (temp == 'quit') or (temp == 'q')):
                    return

                elif( (suffix(temp) == 'C') or (suffix(temp) == 'c') ):
                    print("Celsius:", temp_value(temp))
                    print("Fahrenheit: {0:.1f}".format(cel_to_far(float(temp_value(temp)))))

                elif( (suffix(temp) == 'F') or (suffix(temp) == 'f') ):
                    print("Fahrenheit:", temp_value(temp))
                    print("Celsius: {0:.1f}".format(far_to_cel(float(temp_value(temp)))))

                else:
                    print(error1 + error2 + example)

            except:
               print("Something went wrong and I don't care to fix it.\n")
               return

if(__name__  == '__main__'):
    check_version()
    temperature()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:27:16+00:00Added an answer on May 27, 2026 at 9:27 pm

    The problem is that temperature is being called recursively in different places when an error happens and to exit from the program the user has to enter q/quit as many times as temperature was called.

    To fix the problem, I suggest to remove all recursive calls and handle the errors in a different way. For example, temp might be checked to make sure it’s a correct input from user and if that’s not the case, print the error message and continue the infinite while loop untill is break is called when the input is either q or quit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried to find the answer, but still couldn't.. The table is as follows: id,
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried a bunch of things but I can't get it to work consistently amid
tried to find the answer by googling and in MSDN but with no luck.
Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
Tried to install some program, it gives above message requiring 1.1.4322. BUT as the
tried to research on that but sometimes I seem to lack some googling skills...
As the question says, for some reason my program is not flushing the input
Tried Googling , but couldn't find anything. I have a few files and folders
I know flashing text is banned at many places but still as my client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.