Noob programmer can't figure out simple issue [closed]

Here is my code:

name = input("What is your name? ")
print name + " do you want to play a game?"
answer = input("To play the game, type either yes or no. ")
if answer == yes:
    print "Great," + name + "lets get started!"
elif answer == no:
    print "Okay, good bye!"

import random

number = random.randint(1,50)
guess = input ("Pick a number between 1 and 50. This number cannot be a decimal. ")
if guess > number:
    print "Your guess is too high!"
elif guess < number:
    print "Your guess is too low!"
while guess != number:
    print "Try again!"
else import random
    number = random.randint(1,50)
if guess == number:
    print "You guessed it!"
    print "Great job."
print "Do you want to play again?"

while answer == yes:
    import random

    number = random.randint(1,50)
    guess = input ("Pick a number between 1 and 50. This number cannot be a decimal. ")
    if guess > number:
        print "Your guess is too high!"
    elif guess < number:
        print "Your guess is too low!"
    while guess != number:
        print "Try again!"
    if guess == number:
        print "You guessed it!"
        print "Great job."
    print "Do you want to play again?
elif answer == no:
    print "Okay. Good game " + name + "!"
    print "Play again soon!"

Ok, my first question is why does python not recognize input for the name variable as a string.

The second question is the last elif statement keeps giving me a syntax error. I am not sure why.

The last question is can I loop this code any easier way?


Asked by: Maddie438 | Posted: 27-01-2022






Answer 1

In Python 2x versions, input() takes variable as integer, you could use raw_input() to take it as string.

So basically change your input() to raw_input() for taking the data as string.

In Python 3x versions there is no raw_input, there is only input() and it takes the data as string.

Second question;

elif guess < number:

    print "Your guess is too low!"

while guess != number:

    print "Try again!"

else import random

    number = random.randint(1,50)

This is not a correct syntax, your else needs an if block above itself. You can't use else without an if block.If you think for a second, that makes sense.

Your last question is not fit with SO rules.

Answered by: Catherine515 | Posted: 28-02-2022



Similar questions

C++ or Python for C# programmer?

Closed. This question is opinion-based. It is not c...


Python for C++ or Java Programmer

I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can show if how you were doing this in C++ and how this is done in Python. OK i think i did not put the que...


Python for a Perl programmer

I am an experienced Perl developer with some degree of experience and/or familiarity with other languages (working experience with C/C++, school experience with Java and Scheme, and passing familiarity with many others). I might need to get some web work done in Python (most immediately, related to Google App Engine). As such, I'd like to ask SO overmind for good references on how to best learn Python for someone w...


Perl for a Python programmer

I know Python (and a bunch of other languages) and I think it might be nice to learn Perl, even if it seems that most of the people is doing it the other way around. My main concern is not about the language itself (I think that part is always easy), but about learning the Perlish (as contrasted with Pythonic) w...


python - how real programmer do server loop?

everytime when running this program, I hear my cpu fan is boosting. I suspected the busy waiting while loops in the code is the cause. I wonder how a real programmer will do to optimize this? from multiprocessing import Process, Queue import threading class PThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) #view leave will set this event self.event =...


Python Programmer test

Closed. This question does not meet Stack Overflow guid...


vba - Resources for Python Programmer


Now that Python 2.6 is out, what modules currently in the language should every programmer know about?

A lot of useful features in Python are somewhat "hidden" inside modules. Named tuples (new in Python 2.6), for instance, are found in the collections module. The Library Documentat...


Advice for C# programmer writing Python


C++ or Python for C# programmer?

Closed. This question is opinion-based. It is not c...


Python for C++ or Java Programmer

I have a background in C++ and Java and Objective C programming, but i am finding it hard to learn python, basically where its "Main Function" or from where the program start executing. So is there any tutorial/book which can teach python to people who have background in C++ or Java. Basically something which can show if how you were doing this in C++ and how this is done in Python. OK i think i did not put the que...


java - asm / C / Python / Perl / Lisp / Scheme Programmer looking for something new to learn

Closed. This question does not meet Stack Overflow guid...


Python for a Perl programmer

I am an experienced Perl developer with some degree of experience and/or familiarity with other languages (working experience with C/C++, school experience with Java and Scheme, and passing familiarity with many others). I might need to get some web work done in Python (most immediately, related to Google App Engine). As such, I'd like to ask SO overmind for good references on how to best learn Python for someone w...


Perl for a Python programmer

I know Python (and a bunch of other languages) and I think it might be nice to learn Perl, even if it seems that most of the people is doing it the other way around. My main concern is not about the language itself (I think that part is always easy), but about learning the Perlish (as contrasted with Pythonic) w...


Newbie Python programmer tangling with Lists

Here's what I've got so far: # A. match_ends # Given a list of strings, return the count of the number of # strings where the string length is 2 or more and the first # and last chars of the string are the same. # Note: python does not have a ++ operator, but += works. def match_ends(words): counter = 0 for word in words: if len(word) &gt;= 2 and word[0] == word[-1]: counter += counter retur...


php - Old desktop programmer wants to create S+S project

I have an idea for a product that I want to be web-based. But because I live in a part of the world where the internet is not always available, there needs to be a client desktop component that is available for when the internet is down. Also, I have been a SQL programmer, a desktop application programmer using dBase, VB and Pascal, and I have created simple websites using HTML and website creation tools, such as Frontpage...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top