Jump to content

  • Log In with Google      Sign In   
  • Create Account


Anyone help a python noob?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 balderin   Members   -  Reputation: 102

Like
0Likes
Like

Posted 31 August 2012 - 09:33 PM

Hi! I'm a super noob just starting to learn python and programming in general. My problem is trying to verifiy a random number
but ending up with invalid syntax.

import random

heroAttack = random.randint (1, 10)
if heroAttack > 5
print (You hit!)
else:
print (You miss.)

I always get an invalid syntax at the number 5. Could somebody tell me what is wrong?

Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8162

Like
4Likes
Like

Posted 31 August 2012 - 10:12 PM

You need to end conditions by a colon (:). Also, I think you need "You hit!", "You miss." to be strings, but I could be wrong on this. Also you want to wrap Python code in code tags, because of indentation:


import random
heroAttack = random.randint (1, 10)
if heroAttack > 5:
	 print ("You hit!")
else:
	 print ("You miss.")

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 balderin   Members   -  Reputation: 102

Like
0Likes
Like

Posted 31 August 2012 - 11:10 PM

That was it! Thank you.

#4 Day Dreamer   Members   -  Reputation: 101

Like
0Likes
Like

Posted 01 September 2012 - 12:12 AM

You can shoten thz by:-

import random

if random.randint (1, 10) > 5:
print ("You hit!")
else:
print ("You miss.")

if you not gonna use heroAttack variable anywhere else.

#5 6677   Members   -  Reputation: 1058

Like
0Likes
Like

Posted 01 September 2012 - 09:44 AM

2 things, thz is not a word.
Secondly if he's a noob letting him get used to variables is probably a nice idea. You are correct in that its shorter but who knows. HeroAttack could also be used for a damage calculation later on




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS