python program to dispaly user input from -N to N - python-2.7

Iam new in python programming.I have a following question.
How to display a user input number from -n to n.
Suppose if user enters 2 and it should display -2,-1,0,1,2

Do this :
def f():
n = input("Value n = ?")
for i in range(-n,n+1):
print i
and then call your function

Related

suppress gdb to print command number

In gdb, I would like to print just the command output and NOT the command number. For example, I get
$24 = {timeStamp = 15329666, id = 1, version = 1, checksum = 15329411}
instead I want it to output only
{timeStamp = 15329666, id = 1, version = 1, checksum = 15329411}
I would like to print just the command output and NOT the command number.
The value $24 is not the command number. It's the value history number of that particular output.
Use the output command if you don't want value history.

How do I run my python application daily three times

I have a python application which creates JSON files. I need to execute an application three times per day at midnight, noon and evening.
import json
def add():
a = 10
b = 20
c = a+b
f = open("add.json","w")
f.write(str(c))
def subtract():
a = 10
b = 20
c = b-a
f = open("subtract.json","w")
f.write(str(c))
add()
subtract()
I need to run the application at a specified time automatically
You have 2 possibilities :
1- Let your code run with some timer !
For windows user:
2- use the AT command to create 3 scheduled tasks!
For Linux user:
2- use the Cron command to create 3 scheduled tasks !
which seems to be the best solution in your case!

I need to change the name of an text documente through a cicle

I need that this code create text documents but i need that the name change every time i create a new text document, an example will be that when execute the code it will create a text document named " aventura-1.txt " then why execute again and the name will be " aventura-2.txt" so on too " aventura-n.txt" How i can do that.?
sorry for my bad english btw, this is the code which i have.
import os
def adventure_path ( nombre_aventura) :
if not os. path . isdir (" aventuras ") :
os. mkdir (" aventuras ")
return " aventuras/ %s" %nombre_aventura
archivo = open ( adventure_path ("aventura-n.txt") ,"w")
print "hi"
print "bye"
archivo . close ()
You may use this code that I wrote once and tweaked a little for you...
It will:
check each file in a specific folder
split the names of these files at "." and "-", so we'll be able to catch only the number part of your .txt name (i'm assuming you'll have a specific folder that contains ONLY "aventura-n.txt" named like files)
add this number to a list
get the max value of the numbers list
add +1 to get the next version number
Code:
import os
nums = []
for item in os.listdir('path_to_folder'):
a = item.split(".")[0]
b = a.split("-")[1]
c = int(b)
nums.append(c)
next_num = max(nums) + 1
print next_num

Python input/strings

(Python 2.7.10) So i am a beginner in python, just started learning about a week ago. I need some help with writing the code commented on lines 3 and 5.If the user enters a word instead of a numerical value then I need the program to tell them error and to restart. I commented the program to make it easier to understand. The program works just fine otherwise. Thank you.
## Ask user for age
age = input("Please enter your age.(numerical value)")
## If input is not a numerical value then tell the user "Error. Enter a numerical value"
## Restart program to let the user try again.
## If age is less than 18 then tell them they are too young
if age < 18:
print (" Access denied. Sorry, you are not old enough.")
## If the user is 18 then grant them access
elif age == 18:
print ("Acess granted. You are just old enough to use this program!")
## If user is any age above 18 then grant them access
else:
print ("Access granted.")
his is a way to make sure you get something that can be interpreted as integer from the user:
while True:
try:
# in python 3:
# age = int(input('Please enter your age.(numerical value)'))
# in python 2.7
age = int(raw_input('Please enter your age.(numerical value)'))
break
except ValueError:
print('that was not an integer; try again...')
the idea is to try to cast the string entered by the user to an integer and ask again as long as that fails. if it checks out, break from the (infinite) loop.
Change your age input part to this:
#keep asking for age until you get a numerical value
while True:
try:
age=int(raw_input("Please enter your age.(numerical value)"))
break
except ValueError:
print "Error. Enter a numerical value"

Python: Iterating through a list

I am given a text file that contains many lines like the following... many random information
Spiaks Restaurant|42.74|-73.70|2 Archibald St+Watervliet, NY 12189|http://www.yelp.com/biz/spiaks-restaurant-watervliet|Italian|4|5|4|3|3|4|4
For example, Spiaks Restaurant is in position 0, 42.74 is in position 1, -73.70 is in position 2.... Italian is in position 5...
4|5|4|3|3|4|4 is another list... so basically a list within a list, and the number 4 would be in position 6, 5 in position 7.. etc
I have to ask the user, and the user should reply with:
What type of restaurant would you like => Italian
What is the minimum rating => 3.6
The result should be:
Name: Spiaks Restaurant; Rating 3.86
Name: Lo Portos; Rating 4.00
Name: Verdiles Restaurant; Rating 4.00
Found 3 restaurants.
Here is my code:
rest_type = raw_input("What type of restaurant would you like => ")
min_rate = float(raw_input("What is the minimum rating => "))
def parse_line(text_file):
count = 0.0
a_strip = text_file.strip('\n')
b_split = a_strip.split('|')
for i in range(6, len(b_split)):
b_split[i] = int(b_split[i]) # Takes the current indices in the list and converts it to integers
count += b_split[i] # Add up all of the integers values to get the total ratings
avg_rate = count/len(b_split[6:len(b_split)]) # Takes the total ratings & divides it by the length
#The above basically calculates the average of the numbers like 4|5|4|3|3|4|4
if (rest_type == b_split[5] and avg_rate >= min_rate):
print b_split[0], avg_rate
The problem with the result is.. I get:
None
I know this is a very, very long question, but if someone could give me some insight, I would appreciate it!
Have you trying printing out all of the information you aggregated?
Find out where the error is occurring, be it when you try to parse for specific restaurants, or you just straight up parse nothing and end up with a blank list.