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

  • Home
  • SEARCH
  • 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 4120724
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:15:33+00:00 2026-05-20T23:15:33+00:00

I want to assign something a value and then try and get someone to

  • 0

I want to assign something a value and then try and get someone to guess that value. I have tried something along the lines of this but I can’t seem to get it to work…:

   foo = 1
   guessfoo = input('Guess my number: ')
   if foo == guessfoo:
       print('Well done, You guessed it!')
   if foo != guessfoo:
       print('haha, fail.')

Why doesn’t this work? And how should I be doing it? I am a beginner at this, please help!

  • 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-20T23:15:34+00:00Added an answer on May 20, 2026 at 11:15 pm

    With python version 3 :

    input() returns a ‘str’ (string) object. A string compares to an integer returns False :

    1 == '1'
    False
    

    You must cast your input like that :

    guessfoo = int(input('Guess my number: '))
    

    Don’t forget to try…except if the result of the input cannot be casted into an int.

    Full example code :

    try:
        foo = 1
        guessfoo = int(input('Guess my number: '))
        if foo == guessfoo:
            print('Well done, You guessed it!')
        else:
            print('haha, fail.')
    except ValueError:
        # cannot cast your input
        pass
    

    EDIT:

    With python version 2 :

    Thanks for this comment :

    In previous versions, input would eval the string, so if the user typed in 1, input returned an int.

    Ouput or your original code:

    $ python2 test.py 
    Guess my number: 1
    Well done, You guessed it!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to assign a value from a select box to an input field
I want to assign the value in QString to a const std::string QString qfile(some
I want to assign a NiceMock with the return value of a method. The
I want to assign the array item into variable directly using groovy like this:
I want to assign categories based upon the format of the book that will
I have 4 items in and I want to assign 4 different background-color to
I have case where the class hierarchy is something like this, +---------------+ | UIElement
I have something like this: char *current_day, *current_time; system(date +%F); system(date +%T); It prints
I have a 256-bit value in Verilog: reg [255:0] val; I want to define
This is more like general question: Is there a way to assign a value

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.