2

i'm just writing a simple program that should give me a random integer between 1 and 10 if i input the value of 'r'in code. here's what i have:

import sys, random

from random import *

def randomly():
    return (randint(1, 10))

while True:
    x = input("input string ")
    x = str(x)
    print (x)

    if x == 'r':
        print ("your generated number is", randomly())

    else:
        break

i know there's just something small i'm overlooking. but i can't figure out what. the randomly function works. and if i replace the 'r' conditional with an integer (i used 1) convert x with int(x) and then input 1 the program works fine. like so:

import sys, random

from random import *

def randomly():
    return (randint(1, 10))

while True:
    x = input("input number ")
    x = int(x)
    print (x)

    if x == 1:
        print ("your generated number is", randomly())

    else:
        break

for some reason the if conditional won't respond to my string. i tried x.lower() and it still won't work, while it's true that just using the method i described before with the integer would work fine. it frustrates me that my code won't work the way i want it to. for what it's worth i checked len(x) and it says 2 when i input a single character(r). i'm fairly novice to python but i have the knowledge i need, at least, to write a program like this. any help would be greatly appreciated.

i'm using visual studio 2015 python environment: python 3.2

this is my first question here. i searched to the best of my capabilities and couldn't find an answer. i apologize in advance if there is anything wrong with my question.

  • 3
    Are you getting any error? Also if you are using python 3 input is already a `str`, if you are not using python3 your code would error unless you had a variable `r` defined somehwere – Padraic Cunningham Oct 18 '15 at 22:01
  • Can you check your python version (`import sys; print(sys.version)`)? The `input` builtin behaves differently depending on the version, and the behavior you describe looks like that of python2 actually. – spectras Oct 18 '15 at 22:03
  • What is `print(x)` printing? – JCOC611 Oct 18 '15 at 22:03
  • thank you for the speedy response, there is no error it just executes the else: break. like i said, the code works fine. the if jsut won't execute with any kind of string i input. – PoopSnaggler Oct 18 '15 at 22:04
  • Are you sure you are using python3? What does `sys.version` output as per spectras suggestion output? – Padraic Cunningham Oct 18 '15 at 22:05
  • I'm running the example successfully: `input string r r your generated number is 9 input string r r your generated number is 9 input string r r your generated number is 2 input string r r your generated number is 9 input string ` Running it on `Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin` – jlnabais Oct 18 '15 at 22:05
  • You mention the length is 2 when you only input a single character. I've never used python myself, but can you output which the two different characters are? I suspect one of them might be a newline of some sort, especially if when you print it all you see is the "r". As far as how to handle that, I have no idea other than verifying the first element in your string is "r". – Andrew Shirley Oct 18 '15 at 22:06
  • Works fine for me (using python 3). – Hans Then Oct 18 '15 at 22:07
  • i don't understand what's wrong. print(x) was for me, to check my input. print(sys.version) prints: 3.2 [MSC v.1500 32 bit] – PoopSnaggler Oct 18 '15 at 22:08
  • 1
    What happens if after you convert it to a string, you use the strip command on it? http://stackoverflow.com/questions/1185524/how-to-trim-whitespace-including-tabs – Andrew Shirley Oct 18 '15 at 22:08
  • 1
    This seems more like a visual studio problem than a python one if you are using python3 – Padraic Cunningham Oct 18 '15 at 22:09
  • 1
    => running your program on Python 3.2 on a Ubuntu box gives the expected results. *points at microsoft visual studio* – spectras Oct 18 '15 at 22:10
  • 1
    I've copied your code, and everything works as expected. – Gershon Papi Oct 18 '15 at 22:11
  • 1
    thanks you all, i am so sorry to have wasted your time. i was assuming i did something wrong but many of you claim the code works fine. i'll be trying it in a different IDE. again, thank you for your time. – PoopSnaggler Oct 18 '15 at 22:13
  • That was no waste, it's not like you had a simple typo or something. Plus, question was clear with code samples and showed some research. Keep going. And welcome on SO, by the way. – spectras Oct 18 '15 at 22:21
  • Is there a newline or something that got tacked on without you knowing? Try using `x.strip() == 'r'`. – ddsnowboard Oct 18 '15 at 22:22
  • i got the code to work! using strip() after the input worked for me. must have been a blank space added for some reason. i don't know if it's my keyboard layout or vs2015. but at least i found a workaround. thank you all so much! – PoopSnaggler Oct 18 '15 at 22:23

0 Answers0