What's wrong with my code?
Page 1 of 16 Replies - 882 Views - Last Post: 26 August 2011 - 09:11 AM
#1
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?
Replies To: What's wrong with my code?
#2
Re: What's wrong with my code?
Posted 26 August 2011 - 07:27 AM
What specific problems or errors are you encountering?
#3
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: --> 
That way, we too can run it and find out what's wrong.
It looks to me like your problem is here:
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
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
#4
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.
#5
Re: What's wrong with my code?
Posted 26 August 2011 - 08:18 AM
Dogstopper, on 26 August 2011 - 02:35 PM, said:
In general, it is best to post your code like this: --> 
That way, we too can run it and find out what's wrong.
It looks to me like your problem is here:
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
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
>>>
#6
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote









|