6 Replies - 1444 Views - Last Post: 26 August 2011 - 09:11 AM Rate Topic: -----

#1 iJasonTATE  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 41
  • Joined: 14-August 11

What's wrong with my code?

Posted 26 August 2011 - 07:17 AM

I'm interpreting a little program were you can talk to the computer(I get lonely... JK). Can someone tell me what is wrong with my code?Attached Image
Is This A Good Question/Topic? 1
  • +

Replies To: What's wrong with my code?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,383
  • Joined: 27-December 08

Re: What's wrong with my code?

Posted 26 August 2011 - 07:27 AM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: What's wrong with my code?

Posted 26 August 2011 - 07:35 AM

In general, it is best to post your code like this: --> :code:

That way, we too can run it and find out what's wrong.

It looks to me like your problem is here:
Response = input()
Response = raw_input



Firstly, raw_input() is a function and needs parentheses like I just showed. Secondly, do you really need input() and raw_input()? Pick one. If you are using Python 3, input() is the valid one. In Python 2, raw_input takes in a raw String and input() takes in a valid python expression. Use raw_input() if in doubt on Python 2
Was This Post Helpful? 2
  • +
  • -

#4 iJasonTATE  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 41
  • Joined: 14-August 11

Re: What's wrong with my code?

Posted 26 August 2011 - 08:08 AM

Ohhh soryy I'll do that.

import random

print 'Hello! What is your name?'
myName = raw_input

print 'Hello' + myName + 'How are you doing?'
Response = input()
Response = raw_input

if Response is Good:
    print 'Thats great!'
if Response is Bad:
    print 'Im sorry to hear that.

Was This Post Helpful? 0
  • +
  • -

#5 iJasonTATE  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 41
  • Joined: 14-August 11

Re: What's wrong with my code?

Posted 26 August 2011 - 08:18 AM

View PostDogstopper, on 26 August 2011 - 02:35 PM, said:

In general, it is best to post your code like this: --> :code:

That way, we too can run it and find out what's wrong.

It looks to me like your problem is here:
Response = input()
Response = raw_input



Firstly, raw_input() is a function and needs parentheses like I just showed. Secondly, do you really need input() and raw_input()? Pick one. If you are using Python 3, input() is the valid one. In Python 2, raw_input takes in a raw String and input() takes in a valid python expression. Use raw_input() if in doubt on Python 2

Okay, I'll give it a shot. Thanks.

This is what happens when I try to run the program:

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Hello! What is your name?

Traceback (most recent call last):
  File "C:/Python27/talktoyourcomp.py", line 6, in <module>
    print 'Hello' + myName + 'How are you doing?'
TypeError: cannot concatenate 'str' and 'builtin_function_or_method' objects
>>> 

Was This Post Helpful? 0
  • +
  • -

#6 CrAzD  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 1
  • Joined: 26-August 11

Re: What's wrong with my code?

Posted 26 August 2011 - 08:56 AM

import random

print('Hello! What is your name?')

print('Hello ' + raw_input() + ', How are you doing?')

Response = raw_input().lower()
if Response == 'good':
    print('Thats great!')
if Response == 'bad':
    print('Im sorry to hear that.')




Fixed it to work, can't really go in depth in explanations atm, but if you have questions just post them and I'll check em when I get back on.


What I did
  • Changed is to ==(eqaulto)
  • Changed myName to raw_input(), since myName is only used once, and never again at this point in time, you don't really need to save it to a variable.
  • Added .lower() to raw_input(), this makes everything lowercase so you don't have to worry about Good, GOod, gooD, etc.
  • Removed input()
  • Added () to the end of raw_input
    The way I do print is 2.7 and 3.x compatible, I do it out of habit.

Was This Post Helpful? 2
  • +
  • -

#7 iJasonTATE  Icon User is offline

  • New D.I.C Head

Reputation: -10
  • View blog
  • Posts: 41
  • Joined: 14-August 11

Re: What's wrong with my code?

Posted 26 August 2011 - 09:11 AM

Ohhhh! Thanks man!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1